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 to
post. The
index.jsp page contains the following code.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<s:head />
</head>
<body>
<s:form action="fileUpload" method="post" enctype="multipart/form-data" >
<s:file name="userImage" label="User Image" />
<s:submit />
</s:form>
</body>
</html>
|
Here our file name is userImage. Next we will create our FileUploadAction class. We need to have a File 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.
package vaannila;
import java.io.File;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private File userImage;
private String userImageContentType;
private String userImageFileName;
public String execute()
{
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;
}
}
|
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.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Upload</title>
</head>
<body>
You have uploaded the following file.
<hr>
File Name : ${userImageFileName} <br>
Content Type : ${userImageContentType}
</body>
</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 the defaultStack from the struts-default.xml and paste it in our struts.xml file and rename the stack to fileUploadStack. Our struts.xml file contains the following code.
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"">
<struts>
<package name="fileUploadPackage" extends="struts-default">
<interceptors>
<interceptor-stack name="fileUploadStack">
<interceptor-ref name="exception" />
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="prepare" />
<interceptor-ref name="i18n" />
<interceptor-ref name="chain" />
<interceptor-ref name="debugging" />
<interceptor-ref name="profiling" />
<interceptor-ref name="scopedModelDriven" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload">
<param name="maximumSize">10240</param>
<param name="allowedTypes">image/jpeg,image/gif,image/png</param>
</interceptor-ref>
<interceptor-ref name="checkbox" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="actionMappingParams" />
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError" />
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="fileUpload" class="vaannila.FileUploadAction">
<interceptor-ref name="fileUploadStack" />
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
|
The maximumSize value is set in bytes. Here we set the maximumSize to 10kb. The allowedTypes indicate 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.
War :
阅读(1296) | 评论(0) | 转发(0) |