1

I am struggling hard to display uploaded image using struts2 interceptor.

I searched a lot ,some posts were added to answers that interceptors remove .tmp file of uploaded image. So It gives the INFO as

INFO: Removing file userImage D:\Workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\E-SchoolWeb\upload_1c272ff6_142dc6fc692__8000_00000000.tmp

My image is is uploading successfully to the location

D:\Workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\E-SchoolWeb

But it is not displaying any image...Just image icon is shown as if it is broken link.

So What should I do to display that image.

1) This is my struts.xml code

<action name="userImage" class="com.actions.FileUploadAction" method="execute">
    <interceptor-ref name="fileUpload">
    <param name="allowedTypes">image/jpeg,image/png,image/gif</param>
    <param name="maximumSize">4194304</param>
    </interceptor-ref>
    <interceptor-ref name="params" />
    <interceptor-ref name="validation" />
    <interceptor-ref name="workflow" />
    <interceptor-ref name="defaultStack"></interceptor-ref>
    <result name="success" type="redirect">SuccessUserImage.jsp</result>
    <result name="input">userRegister.jsp</result>
</action>

<s:submit value="Upload" align="center" />
</s:form>

2) Action class

private static final long serialVersionUID = 1L;
private File userImage;
private String userImageContentType;
private String userImageFileName;

private HttpServletRequest servletRequest;

ImageDAO idao=new ImageDAO();

public String execute() {
    try {       
        String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
        System.out.println("Server path:" + filePath);

        File fileToCreate = new File(filePath,this.userImageFileName);

        System.out.println("fileToCreate="+fileToCreate.getName());
        FileUtils.copyFile(this.userImage, fileToCreate);

        // call function in DAO to save image in  database;;;;;;

        boolean res=idao.saveProfileImage(filePath+"\\"+fileToCreate.getName());

        //read image method call code...........
        if(res == true){

            //call read method...1) get blob from db i.e. binary stream2) then create outputstream then pass it to jsp after writing to it.
        //3) and display..
         }

        //read call end

        if(res == true){
        return SUCCESS;
        }

    } catch (Exception e) {
        e.printStackTrace();
        addActionError(e.getMessage());

        return INPUT;
    }

    return SUCCESS;
}

public File getUserImage() {
    return userImage;
}

public void setUserImage(File userImage) {
    this.userImage = userImage;
}

public String getUserImageContentType() {
    return userImageContentType;
}

public void setUserImageContentType(String userImageContentType) {
    this.userImageContentType = userImageContentType;
}

public String getUserImageFileName() {
    return userImageFileName;
}

public void setUserImageFileName(String userImageFileName) {
    this.userImageFileName = userImageFileName;
}

@Override
public void setServletRequest(HttpServletRequest servletRequest) {
    this.servletRequest = servletRequest;        
}

Hope you got the scenario.

Thanks in advance.

HpTerm
  • 8,151
  • 12
  • 51
  • 67
Coder
  • 116
  • 1
  • 15
  • 1
    First, you should move it to somewhere *outside* of the web app. Second, you should stream it back using a stream result. – Dave Newton Dec 10 '13 at 12:52
  • Are you sure of the file path ? "D:\Workspace.metadata.plugins\" it looks like "\" escape is missing. Should not be "D:\Workspace\.metadata\.plugins" instead? – Kloe2378231 Dec 10 '13 at 12:55
  • possible duplicate of http://stackoverflow.com/questions/15269541/how-to-send-and-display-qrcode-image-from-action-class-to-jsp-in-struts-2 – Roman C Dec 10 '13 at 13:43
  • Yes the path is correct. I see the uploaded images in **D:\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps \E-SchoolWeb** . – Coder Dec 11 '13 at 04:53
  • Hey I got a problem. my index.jsp is in Web-Content Folder,and all image uploading files were in Web-Content\jsp\ folder. struts2 may not be getting correct path to display image. So I moved all image uploading jsp files out of jsp folder and it worked great.Thank for all your suggestions and sorry to confuse you. – Coder Dec 11 '13 at 11:55

0 Answers0