Solved

StreamResult and Struts2 Image uploading to jsp : How To

Posted on 2010-11-23
2
1,941 Views
Last Modified: 2013-11-24
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
Comment
Question by:yojimbojim
  • 2
2 Comments
 
LVL 7

Expert Comment

by:techsathish
ID: 34204162
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
 
LVL 7

Accepted Solution

by:
techsathish earned 500 total points
ID: 34204170
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
changeXy challenge 13 89
why cannot we forward request once the response is committed 2 46
tomcat startup error 5 106
jmss example java 2 24
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question