Note that there are some explanatory texts on larger screens.

plurals
  1. POUploaded Image does not display in JSP using Struts2
    text
    copied!<p>I am struggling hard to display uploaded image using struts2 interceptor.</p> <p>I searched a lot ,some posts were added to answers that interceptors remove .tmp file of uploaded image. So It gives the INFO as </p> <blockquote> <p>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</p> </blockquote> <p>My image is is uploading successfully to the location </p> <blockquote> <p>D:\Workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\E-SchoolWeb</p> </blockquote> <p>But it is not displaying any image...Just image icon is shown as if it is broken link.</p> <p>So What should I do to display that image.</p> <p>1) This is my struts.xml code</p> <pre><code>&lt;action name="userImage" class="com.actions.FileUploadAction" method="execute"&gt; &lt;interceptor-ref name="fileUpload"&gt; &lt;param name="allowedTypes"&gt;image/jpeg,image/png,image/gif&lt;/param&gt; &lt;param name="maximumSize"&gt;4194304&lt;/param&gt; &lt;/interceptor-ref&gt; &lt;interceptor-ref name="params" /&gt; &lt;interceptor-ref name="validation" /&gt; &lt;interceptor-ref name="workflow" /&gt; &lt;interceptor-ref name="defaultStack"&gt;&lt;/interceptor-ref&gt; &lt;result name="success" type="redirect"&gt;SuccessUserImage.jsp&lt;/result&gt; &lt;result name="input"&gt;userRegister.jsp&lt;/result&gt; &lt;/action&gt; &lt;s:submit value="Upload" align="center" /&gt; &lt;/s:form&gt; </code></pre> <p>2) Action class</p> <pre><code>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; } </code></pre> <p>Hope you got the scenario.</p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload