Required JAR file
Before we start, we need to make sure commons-io.jar file is present in the classpath. Following are the list of required Jar files.Getting Started
In order to add file upload functionality we will add an action class FileUploadAction to our project. Create fileFileUploadAction.java
in package com.blogspot.rajvishwakarma15
.struts2
.FileUploadAction.java
package
import java.io.File; import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.FileUtils; import org.apache.struts2.interceptor.ServletRequestAware; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends ActionSupport implements ServletRequestAware { private File userImage; private String userImageContentType; private String userImageFileName; private HttpServletRequest servletRequest; public String execute() { try { String filePath = servletRequest.getSession().getServletContext().getRealPath(“/”); System.out.println( "Server path:" + filePath); File fileToCreate = new File(filePath, this .userImageFileName); FileUtils.copyFile( this .userImage, fileToCreate); } 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; } } |
private File userImage;
-> This will store actual uploaded Fileprivate String userImageContentType;
-> This string will contain the Content Type of uploaded file.private String userImageFileName;
-> This string will contain the file name of uploaded file.
private File uploadedFile
, the content type will be uploadedFileContentType and file name uploadedFileFileName.Also note in above action class, we have implemented interface org.apache.struts2.interceptor.ServletRequestAware. This is to get servletRequest object. We are using this path to save the uploaded file in
execute()
method. We have used FileUtil.copyFile()
method of commons-io package to copy the uploaded file in root folder.
This file will be retrieved in JSP page and displayed to user.The JSPs
Create two JSP file in WebContent folder. UserImage.jsp will display a form to user to upload image. On submit, the file will be uploaded and saved on server. User will be sent to SuccessUserImage.jsp file where File details will be displayed. Copy following code into it.UserImage.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> < html > < head > < title >Upload User Image</ title > </ head > < body > < h2 >Struts2 File Upload & Save Example</ h2 > < s:actionerror /> < s:form action = "userImage" method = "post" enctype = "multipart/form-data" > < s:file name = "userImage" label = "User Image" /> < s:submit value = "Upload" align = "center" /> </ s:form > </ body > </ html > |
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> < html > < head > < title >Success: Upload User Image</ title > </ head > < body > < h2 >Struts2 File Upload Example</ h2 > User Image: < s:property value = "userImage" /> < br /> Content Type: < s:property value = "userImageContentType" /> < br /> File Name: < s:property value = "userImageFileName" /> < br /> Uploaded Image: < br /> < img src = "<s:property value=" userImageFileName"/>"/> </ body > </ html > |
Struts.xml entry
Add following entry for FileUploadAction class to struts.xml file.< action name = "userImage" class = "com.blogspot.rajvishwakarma15.struts2.FileUploadAction" > < interceptor-ref name = "fileUpload" > < param name = "maximumSize" >2097152</ param > < param name = "allowedTypes" > image/png,image/gif,image/jpeg,image/pjpeg </ param > </ interceptor-ref > < interceptor-ref name = "defaultStack" ></ interceptor-ref > < result name = "success" >SuccessUserImage.jsp</ result > < result name = "input" >UserImage.jsp</ result > </ action > |
The file upload interceptor also does the validation and adds errors, these error messages are stored in the struts-messsages.properties file. The values of the messages can be overridden by providing the text for the following keys:
- struts.messages.error.uploading – error when uploading of file fails
- struts.messages.error.file.too.large – error occurs when file size is large
- struts.messages.error.content.type.not.allowed – when the content type is not allowed
That’s All Folks
Compile and Execute the project in eclipse and goto link http://localhost:8080/StrutsHelloWorld/UserImage.jspImage Upload Screen
Image Upload Screen in case of error
Image Upload Screen on success
No comments:
Post a Comment