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.