Solved

StreamResult and Struts2 Image uploading to jsp : How To

Posted on 2010-11-23
2
1,928 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.

772 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