Results 1 to 4 of 4
  1. #1
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default Need help in logic

    I have a word cloud app which generates a image based upon user input text. This generated image is stored in images folder. I have provided a save image option which allows user to save this image on their desktop. I am doing this by calling a servlet shown below which reads the image stored & writes to byte stream.
    Java Code:
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		
    		response.setHeader("Content-disposition","attachment; filename=output.png");
    		
    		String filename = request.getSession().getServletContext().getRealPath("/")+"/images/output.png";
    		String mimeType = "image/png";
    		response.setContentType(mimeType);
    		
    	    File file = new File(filename);
    	    response.setContentLength((int)file.length());
    	    FileInputStream in = new FileInputStream(file);
    	    OutputStream outputStream = response.getOutputStream();
    
    	    byte[] buf = new byte[1024];
    	    int count = 0;
    	    while ((count = in.read(buf)) >= 0) {
    	    	outputStream.write(buf, 0, count);
    	    }
    	    in.close();
    	    outputStream.close();
    
    		
    	}
    Problem with this approach is, if another user (user 2) meantime generates a word cloud, it replaces output.png with this new image. Now if user 1 tries to save the image, he gets an wrong image(image of user 2).
    Can you please suggest me a better approach which will allow to keep images user specific?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Don't store it in a folder, use a database instead?
    Is there a way of identifying a particular user?
    Do they log in?
    Or is it only via a session id?

  3. #3
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    yes, i have the user logged in details. But I guess storing every image in the DB would be an overhead. Is it possible to store image in a session & then retrieve it while saving it. If yes, please let me know how.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Storing in a db (which is essentially disk) is far preferable than trying to hold images in memory.

    Provide a single slot in the db to hold an image against the logged in user.

    Why are you storing the image anyway? I don't know the flow in your system so I could well be giving bad advice here.

Similar Threads

  1. Formula Logic Help
    By sehudson in forum New To Java
    Replies: 4
    Last Post: 03-11-2011, 04:17 AM
  2. Need help on logic
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 03-10-2011, 11:06 AM
  3. Logic not working
    By Prajin in forum AWT / Swing
    Replies: 1
    Last Post: 07-19-2010, 07:54 PM
  4. need a logic for this
    By rajivjoshi in forum New To Java
    Replies: 4
    Last Post: 06-12-2010, 02:18 PM
  5. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 12:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •