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

Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need help on Clonning objects in Java 4 40
wordsWithout 49 77
mergeTwo  challenge 13 70
maven project error 5 16
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 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…
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.

762 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

6 Experts available now in Live!

Get 1:1 Help Now