Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 16th, 2012  |  syntax: None  |  size: 1.30 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Display servlet as img src using session attribute
  2. <img src="/displaySessionImage?widgetName=something"/>
  3.        
  4. protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  5.  
  6.     HttpSession session = request.getSession();
  7.     String widgetName = request.getParameter("widgetName");    
  8.  
  9.     try {
  10.                     //this is my file manager which was store ealier
  11.         StorageFile file = (StorageFile)session.getAttribute(widgetName);      
  12.         response.setContentType(file.getContentType());        
  13.  
  14.                     //the file manager can retrieve input stream
  15.         InputStream in  = file.getInputStream();
  16.         OutputStream outImage = response.getOutputStream();
  17.  
  18.         byte[]  buf   = new byte[1024];
  19.         int     count = 0;
  20.         while ((count = in.read(buf)) >= 0) {
  21.             outImage.write(buf, 0, count);
  22.         }
  23.  
  24.  
  25.     } catch (Exception e) {
  26.         // TODO Auto-generated catch block
  27.         e.printStackTrace();
  28.     }
  29.  }
  30.        
  31. InputStream input = uploadedFile.getInputStream();
  32. ByteArrayOutputStream output = new ByteArrayOutputStream();
  33. // Copy bytes from input to output the usual way.
  34. // ...
  35.  
  36. byte[] content = output.toByteArray();
  37. // Now store it in session.
  38.        
  39. // ...
  40. response.getOutputStream().write(content);