Tuesday, August 6, 2013

Struts 2 File Upload Tutorial

In this example you will learn how to do file upload with the help of the built-in FileUploadInterceptor. To do this first we need to get the file form the user. We use the Struts 2 tags to build our form. The encoding type of the form should be set to multipart/form-data and the HTTP method should be set topost. The index.jsp page contains the following code.
01.<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
02.pageEncoding="ISO-8859-1"%>
03.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04.<%@taglib uri="/struts-tags"  prefix="s" %>
05.<html>
06.<head>
07.<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
08.<title>Insert title here</title>
09.<s:head />
10.</head>
11.<body>
12.<s:form action="fileUpload" method="post" enctype="multipart/form-data">
13.<s:file name="userImage" label="User Image" />
14.<s:submit />
15.</s:form>
16.</body>
17.</html>
Here our file name is userImage. Next we will create our FileUploadAction class. We need to have aFile attribute and the corresponding getter and setter methods in the action to receive the userImage file.
You will also get information regarding the file name and content type of the file if you implement the following setter methods. This step is optional if you implement the setter methods you will get more details regarding the file.
1.public void setUserImageContentType(String userImageContentType) {
2.this.userImageContentType = userImageContentType;
3.}
4.public void setUserImageFileName(String userImageFileName) {
5.this.userImageFileName = userImageFileName;
6.}
The FileUploadAction extends ActionSupport and contains the following code.
01.package vaannila;
02. 
03.import java.io.File;
04. 
05.import com.opensymphony.xwork2.ActionSupport;
06. 
07.public class FileUploadAction extends ActionSupport{
08. 
09.private File userImage;
10. 
11.private String userImageContentType;
12. 
13.private String userImageFileName;
14. 
15.public String execute()
16.{
17.return SUCCESS;
18.}
19. 
20.public File getUserImage() {
21.return userImage;
22.}
23. 
24.public void setUserImage(File userImage) {
25.this.userImage = userImage;
26.}
27. 
28.public String getUserImageContentType() {
29.return userImageContentType;
30.}
31. 
32.public void setUserImageContentType(String userImageContentType) {
33.this.userImageContentType = userImageContentType;
34.}
35. 
36.public String getUserImageFileName() {
37.return userImageFileName;
38.}
39. 
40.public void setUserImageFileName(String userImageFileName) {
41.this.userImageFileName = userImageFileName;
42.}
43. 
44.}
When the file is uploaded successful, the user will be forwarded to the success page, where the details regarding the file upload will be displayed. The success.jsp page contains the following code.
01.<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
02.pageEncoding="ISO-8859-1"%>
03.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04.<html>
05.<head>
06.<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
07.<title>File Upload</title>
08.</head>
09.<body>
10.You have uploaded the following file.
11.<hr>
12.File Name : ${userImageFileName}
13. 
14.Content Type : ${userImageContentType}
15.</body>
16.</html>
Now let's run the example.
When the user selects an image file and upload it. The following success page will be displayed to the user.
We only need an image file, what if the user uploads a pdf document? Let's see how to validate this in the next page.
We can validate either programmatically or declaratively. Let's see how to do programmatically first. Validate the file in the validate() method of the FileUploadAction class. You can get the file using the getX() method and the file type type using the getXContentType() method.
In this example we validate declaratively. To do this we need to create our own interceptor stack. The only change we need to do is to add few parameters to the fileUpload interceptor. So we copy thedefaultStack from the struts-default.xml and paste it in our struts.xml file and rename the stack tofileUploadStack. Our struts.xml file contains the following code.
01.<!DOCTYPE struts PUBLIC
02."-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04. 
05. 
06.<struts>
07.<package name="fileUploadPackage" extends="struts-default">
08.<interceptors>
09.<interceptor-stack name="fileUploadStack">
10.<interceptor-ref name="exception" />
11.<interceptor-ref name="alias" />
12.<interceptor-ref name="servletConfig" />
13.<interceptor-ref name="prepare" />
14.<interceptor-ref name="i18n" />
15.<interceptor-ref name="chain" />
16.<interceptor-ref name="debugging" />
17.<interceptor-ref name="profiling" />
18.<interceptor-ref name="scopedModelDriven" />
19.<interceptor-ref name="modelDriven" />
20.<interceptor-ref name="fileUpload">
21.<param name="maximumSize">10240</param>
22.<param name="allowedTypes"> image/jpeg,image/gif,image/png</param>
23.</interceptor-ref>
24.<interceptor-ref name="checkbox" />
25.<interceptor-ref name="staticParams" />
26.<interceptor-ref name="actionMappingParams" />
27.<interceptor-ref name="params">
28.<param name="excludeParams"> dojo\..*,^struts\..*</param>
29.</interceptor-ref>
30.<interceptor-ref name="conversionError" />
31.<interceptor-ref name="validation">
32.<param name="excludeMethods"> input,back,cancel,browse</param>
33.</interceptor-ref>
34.<interceptor-ref name="workflow">
35.<param name="excludeMethods"> input,back,cancel,browse</param>
36.</interceptor-ref>
37.</interceptor-stack>
38.</interceptors>
39. 
40.<action name="fileUpload" class="vaannila.FileUploadAction">
41.<interceptor-ref name="fileUploadStack" />
42.<result name="input">/index.jsp</result>
43.<result name="success">/success.jsp</result>
44.</action>
45.</package>
46.</struts>
The maximumSize value is set in bytes. Here we set the maximumSize to 10kb. The allowedTypesindicate the file types that can be uploaded. Here we set it to only image files like image/jpeg,image/gif,image/png.
Now lets see how the validation works. We will upload a text file. The following error message is displayed to the user.
Now we will upload a file greater than 10kb, the following error message will be displayed.

No comments:

Post a Comment