Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1996
  • Last Modified:

StreamResult and Struts2 Image uploading to jsp : How To

I have an application where I want to use a streamresult in struts2 to serve blobs as images on a
jsp page.

If somebody could point me to a good example I would appreciate it.
0
yojimbojim
Asked:
yojimbojim
  • 2
1 Solution
 
techsathishCommented:
public class FileUploadAction implements Serializable {
      byte[] image;

      public byte[] getImage() {
            return image;
      }

      public void setImage(byte[] image) {
            this.image = image;
      }

      /** Don't invoke this. Used by Hibernate only. */
      public void setImageBlob(Blob imageBlob) {
            this.image = this.toByteArray(imageBlob);
      }

      /** Don't invoke this. Used by Hibernate only. */
      public Blob getImageBlob() {
            return Hibernate.createBlob(this.image);
      }

      private byte[] toByteArray(Blob fromBlob) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                  return toByteArrayImpl(fromBlob, baos);
            } catch (SQLException e) {
                  throw new RuntimeException(e);
            } catch (IOException e) {
                  throw new RuntimeException(e);
            } finally {
                  if (baos != null) {
                        try {
                              baos.close();
                        } catch (IOException ex) {
                        }
                  }
            }
      }

      private byte[] toByteArrayImpl(Blob fromBlob, ByteArrayOutputStream baos)
                  throws SQLException, IOException {
            byte[] buf = new byte[4000];
            InputStream is = fromBlob.getBinaryStream();
            try {
                  for (;;) {
                        int dataSize = is.read(buf);

                        if (dataSize == -1)
                              break;
                        baos.write(buf, 0, dataSize);
                  }
            } finally {
                  if (is != null) {
                        try {
                              is.close();
                        } catch (IOException ex) {
                        }
                  }
            }
            return baos.toByteArray();
      }
}

Use this Action class and create the proper hibernate file..
0
 
techsathishCommented:
Use struts tag

<s:file name="upload" label="File"/>

public class StrutsFileUpload extends ActionSupport {
 
    private File upload;//The actual file  
    private String uploadContentType; //The content type of the file
    private String uploadFileName; //The uploaded file name
    private String caption;//The caption of the file entered by user
   
    public String execute() throws Exception {
          ActionContext ctx= ActionContext.getContext();      
        HttpServletRequest fpmsrequest =(HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
        //String strFile = fpmsrequest.getParameter("upfile").toString();
        System.out.println(" File " + getUpload().getAbsoluteFile() );
          return SUCCESS;
    }
  public String getCaption() {
            return caption;
      }

      public void setCaption(String caption) {
            this.caption = caption;
      }

public File getUpload() {
    return upload;
  }
  public void setUpload(File upload) {
    this.upload = upload;
  }
  public String getUploadContentType() {
    return uploadContentType;
  }
  public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
  }
  public String getUploadFileName() {
    return uploadFileName;
  }
  public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
  }
 
}

This example shows how to upload a image using Struts 2... Any doubt just let me know..
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now