Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


StreamResult and Struts2 Image uploading to jsp : How To

Posted on 2010-11-23
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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

715 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