Note that there are some explanatory texts on larger screens.

plurals
  1. POImage transmission over HTTP with Java
    text
    copied!<p>Hope the Java experts in stackoverflow can shed some light :</p> <p>We are successfully sending images by serializing the image ( after casting it to Java ImageIcon Serializable type), but the Serialized IO seems expensive ( readObject(), writeObject()), as we keep hitting the "java.lang.OutOfMemoryError: Java heap space" errors in the servlet container.</p> <p>Is there a better way of transmitting image files in Java over HTTP ?</p> <p>The application is using Java Robot class to send desktop images continually to multiple clients over a servlet ( to facilitate the HTTP transmission). Sounds like good advice already, let me post some of the code to give you an idea for my project. I have to use Java ( over the web that is using servlets), and my job depends on this working out ...please help ...</p> <p><em><strong></em>__</strong><em>Adding critical code excerpts from the components <strong></em>__<em>_</em>__<em>_</em>__<em>_</em>__</strong></p> <p>I. ClientApplet (Captures desktop images(sends serialized over sockets to a central ImageBroker)</p> <p>-> II. ImageBroker (Reads Serialized images from ClientApplet over sockets and sends it to servlet to make it available to HTTP viewer applets)</p> <p>-> III. ViewerServlet Forward the images to applets over HTTP</p> <p>-> IV.Applet reading serialized images</p> <pre><code>// I. Client Applet // (sends serialized desktop images to ImageBroker server over TCP sockets ) class ScreenShoot extends Thread { Socket socket = null; Robot robot = null; // Used to capture screen Rectangle rectangle = null; //Used to represent screen dimensions boolean continueLoop = true; //Used to exit the program public ScreenShoot(Socket socket, Robot robot, Rectangle rect) { this.socket = socket; this.robot = robot; rectangle = rect; start(); } public void run() { ObjectOutputStream oos = null; //Used to write an object to the streem try { //Prepare ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); } catch (IOException ex) { ex.printStackTrace(); } String Userid = "test"; String ConfId = "ONE"; MyClass sendHeader = new MyClass(Userid,ConfId, 300, 300, 20, 30); System.out.println("sendHeader: " + sendHeader); // Send the header (username, ConferenceID) first before sending the images try { oos.writeObject(sendHeader); System.out.println("sent HEADER object1: "); } catch (IOException ex) { ex.printStackTrace(); } int countRec = 1; while (continueLoop) { //Capture screen BufferedImage image = robot.createScreenCapture(rectangle); /* I have to wrap BufferedImage with ImageIcon because BufferedImage class * does not implement Serializable interface */ ImageIcon imageIcon = new ImageIcon(image); //Send captured screen to the server try { System.out.println("ScreenSpyer:before sending image-writeObject"); // oos.writeObject(imageIcon); oos.writeUnshared(imageIcon); countRec++; if (countRec &gt; 20 ) { oos.reset(); //Clear ObjectOutputStream cache countRec = 1; } System.out.println("ScreenSpyer: New screenshot sent"); } catch (IOException ex) { ex.printStackTrace(); } //wait for 1000ms to redu/ e network traffic try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } </code></pre> <hr> <p>II.Central ImageBroker Server Broker handles images from various senders and traffics them to the right viewers ( coming through a servlet) -Receives images from client screensender applet over TCP sockets - Forwards Serialized images to ViewerServlet</p> <pre><code>// Section READING the incoming images ( this is over simple TCP Sockets) class HandleSenders extends Thread { private Socket socket = null; private WinTest mainprog; private MessageBin source; private InputStream fromServer = null; public HandleSenders(WinTest mainprog, Socket socket, MessageBin source) { super("HandleSenders"); this.socket = socket; this.mainprog = mainprog; this.source = source; } // getNextMessage() returns the next new message. // It blocks until there is one. public String getNextMessage() { // Create a message sink to wait for a new message from the // message source. return new MessageHold().getNextMessage(source); } public void run() { // Reading Images from SnreenSender client ObjectInputStream in = null; byte[] buffer = new byte[256]; int fromServer_val, backup_val =0 ; try { // in = new ObjectInputStream(socket.getInputStream()); fromServer = socket.getInputStream(); in = new ObjectInputStream(fromServer); boolean forever = true; int cntr = 1; boolean contin = true; int counter = 1; boolean Found = false; String theMessage = ""; while (contin) { // if (cntr++ &gt; 5) break; // fromServer_val = fromServer.read(buffer); // dealing with null values which crashes the system, // if for some problem in transmission you get a null, we just put the // previous valid data // into the null one. ImageIcon imageIcon = (ImageIcon)in.readObject(); Found = mainprog.FindViewersAndSend(senderHeader.confid, imageIcon); // Blocks first time and only if the structure has no // clients for this sender if (!Found) theMessage = getNextMessage(); //blocks </code></pre> <p>Section handling SENDING the images to servlets</p> <pre><code> public boolean FindViewersAndSend(String confid, ImageIcon imageIcon) { Iterator it = socketMap.entrySet().iterator(); ObjectOutputStream viewerOut = null; byte[] buffer = new byte[256]; Socket viewerClient = null; OutputStream toClient = null; boolean Found = false; int recordCount = 1; try { while (it.hasNext()) { // Iterating through structures of Viewer servlet socket connections Map.Entry pairs = (Map.Entry)it.next(); socketUserStruct = getSocketDetails((String)pairs.getKey()); if ( socketUserStruct != null &amp;&amp; !socketUserStruct.equals ("")) { StructConfId = (String)socketUserStruct.getConfId(); StructUserId = (String)pairs.getKey(); } if (StructConfId.equals(confid)) { Found = true; viewerOut = (ObjectOutputStream)socketUserStruct.getObjectOutputStream(); // write the serialized data to the servlet.... // which in turn sends it to the applet over http // viewerOut.writeObject(imageIcon); viewerOut.writeUnshared(imageIcon); recordCount++; if (recordCount &gt; 10) { viewerOut.flush(); recordCount = 0; } } } } catch (IOException e) { /// clean up removeSocketClient(StructUserId); } return Found; </code></pre> <p>}</p> <hr> <p>III. ViewerServlet - Getting images from Image broker and sends the Serialized image to Applets ( over http)</p> <pre><code>public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, Socket echoSocket = null; PrintWriter out = null; InputStream fromServer = null; byte[] buffer = new byte[256]; int fromServer_val, backup_val = 0; // OutputStream toClient = res.getOutputStream(); try { echoSocket = new Socket("localhost", RES_PORT); out = new PrintWriter(echoSocket.getOutputStream(), true); String screensize = "300"; String viewerHeader = userid+","+confid+","+screensize; out.println(viewerHeader); out.println("bye"); out.flush(); InputStream is = echoSocket.getInputStream(); ObjectInputStream in = new ObjectInputStream(is); ObjectOutputStream oos = new ObjectOutputStream(res.getOutputStream()); try { int recordCount = 1; while (true) { ImageIcon imageIcon = (ImageIcon)in.readObject(); oos.writeObject(imageIcon); if (recordCount++ &gt; 5) { recordCount = 1; oos.flush(); } // oos.flush(); } } catch (ClassNotFoundException e) { ....... } catch (IOException ex) { ex.printStackTrace(); } } catch (IOException e) { // show("plainViewAdapter-doGet:4"); fromServer.close(); } } </code></pre> <hr> <p>IV. Viewer Applet ( Connecting to the Viewer Servlet to receive the images )</p> <pre><code>public void start() { try { String url_string = "http://" + winsellahost + ":" + winsellaport + "/BrowserShare /viewerproxyservlet" + "?userid=viewer1&amp;confid=ONE"; URL url = new URL(url_string); InputStream is = url.openConnection().getInputStream(); ObjectInputStream in = new ObjectInputStream(is); boolean firstTime = true; while (continueLoop) { show("5"); //Recve client screenshot and resize it to the current panel size ImageIcon imageIcon = (ImageIcon)in.readObject(); // in.reset(); Image image = imageIcon.getImage(); image = image.getScaledInstance(cPanel.getWidth(), cPanel.getHeight(), Image.SCALE_FAST); //Draw the received screenshot } in.close(); is.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload