StreamResult and Struts2 Image uploading to jsp : How To

Posted on 2010-11-23
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.
Question by:yojimbojim
  • 2

Expert Comment

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 {
                        } 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 =;

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

Use this Action class and create the proper hibernate file..

Accepted Solution

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..

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now