Welcome to Part-6 of 7-part series of Struts2 Framework. In previous part we went through basics of Struts2 Interceptors. Also we created a custom interceptor and integrated it through Struts2 application.
It is strongly recommended to go through previous articles in case you new to Struts2 Framework.
Today we will see how to do File Upload in Struts2. We will use Struts2 built-in FileUploadInterceptor in our example to upload the file. The Struts 2 File Upload Interceptor is based on MultiPartRequestWrapper, which is automatically applied to the request if it contains the file element.
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.
In order to add file upload functionality we will add an action class FileUploadAction to our project. Create file FileUploadAction.java in package net.viralpatel.struts2.
In above class file we have declared few attributes:
private File userImage; -> This will store actual uploaded File
private 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.
The fields userImageContentType and userImageFileName are optional. If setter method of these fields are provided, struts2 will set the data. This is just to get some extra information of uploaded file. Also follow the naming standard if you providing the content type and file name string. The name should be ContentType and FileName. For example if the file attribute in action file is 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.
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.
<%@pagecontentType="text/html; charset=UTF-8"%><%@taglibprefix="s"uri="/struts-tags"%><html><head><title>Upload User Image</title></head><body><h2>Struts2 File Upload & Save Example</h2><s:actionerror /><s:formaction="userImage"method="post"enctype="multipart/form-data"><s:filename="userImage"label="User Image" /><s:submitvalue="Upload"align="center" /></s:form></body></html>
Note that in above entry we have specified two parameter to fileUpload interceptor, maximumSize and allowedTypes. These are optional parameters that we can specify to interceptor. The maximumSize param will set the maximum file size that can be uploaded. By default this is 2MB. And the allowedTypes param specify the allowed content types of file which can be uploaded. Here we have specified it to be an image file (image/png,image/gif,image/jpeg,image/pjpeg).
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.jsp
Image Upload Screen
Image Upload Screen in case of error
Image Upload Screen on success