1.文件上传的Action,FileUploadAction
这里演示的是可以一次上传多个文件的示例,数量没有限制
package org.liky.struts.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* 注意按照FileUploadInterceptor属性的定义要求这里有三个属性是固定的 1.File file 2.[File
* Name]ContentType 3.[File Name]FileName
*
* File Name等同于<input type="file" name="File Name">中的File Name 例如<input
* type="file" name="file">那么属性相应的就是 1.File file 2.String fileContentType
* 3.String fileFileName
*
* @author Abu
*
*/
public class FileUploadAction extends ActionSupport {
private static final long serialVersionUID = -7025671119418195167L;
private String username;
private String password;
private int age;
private File[] file;
private String[] fileContentType;
private String[] fileFileName;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
public String[] getFileContentType() {
return fileContentType;
}
public void setFileContentType(String[] fileContentType) {
this.fileContentType = fileContentType;
}
public String[] getFileFileName() {
return fileFileName;
}
public void setFileFileName(String[] fileFileName) {
this.fileFileName = fileFileName;
}
@Override
public String execute() throws Exception {
String path = ServletActionContext.getRequest().getRealPath("/upload");
for (int i=0;i<file.length; i++) {
// 构造输入字节流
InputStream is = new FileInputStream(file[i]);
// 输出文件,fileFileName由struts2自动注入
File destFile = new File(path, this.getFileFileName()[i]);
OutputStream os = new FileOutputStream(destFile);
int length = 0;
byte[] buffer = new byte[1024];
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.close();
is.close();
}
return ActionSupport.SUCCESS;
}
}
2.文件上传的jsp页面,fileupload.jsp
注意红色部分,这里必须制定,表单数据的提交方式为post,而且格式为multipart/form-data
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("UTF-8");%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>测试文件上传</title>
</head>
<body>
<h2>测试文件上传</h2>
<form action="fileUpload.action" name="UploadForm" method="post" enctype="multipart/form-data">
姓名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
年龄:<input type="text" name="age"><br>
文件:<input type="file" name="file"><br>
文件:<input type="file" name="file"><br>
文件:<input type="file" name="file"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
3.后期表单处理的jsp页面,showUser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login result</title>
</head>
<body>
<h3 align="center" style="color:red;">After handle the file upload, show user information</h3>
<hr>
Your username:${requestScope.username }<br>
Your password:${requestScope.password }<br>
Your age:${requestScope.age }<br>
</body>
</html>
4.配置文件,struts.xml
注意你只需要配置红色部分的action就可以了,因为懒的原因所以其他的interceptor和action就没有去删除了
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<interceptors>
<interceptor name="sampleInterceptor"
class="org.liky.struts.interceptor.SampleInterceptor">
<param name="param">liky green</param>
</interceptor>
<interceptor name="anotherInterceptor"
class="org.liky.struts.interceptor.AnotherInterceptor">
</interceptor>
<interceptor-stack name="sampleInterceptorStack">
<interceptor-ref name="sampleInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="anotherInterceptor"></interceptor-ref>
</interceptor-stack>
<interceptor name="myMethodInterceptor"
class="org.liky.struts.interceptor.MyMethodInterceptor">
</interceptor>
<interceptor name="authInterceptor"
class="org.liky.struts.interceptor.AuthInterceptor">
</interceptor>
</interceptors>
<default-interceptor-ref name="sampleInterceptorStack">
</default-interceptor-ref>
<!-- Global Results -->
<global-results>
<result name="login">/login.jsp</result>
</global-results>
<action name="login"
class="org.liky.struts.action.LoginAction">
<result name="success">/register.jsp</result>
<result name="input">login.jsp</result>
</action>
<action name="convert"
class="org.liky.struts.action.ConvertAction">
<result name="success">/output.jsp</result>
<result name="input">/converter.jsp</result>
</action>
<action name="convertList"
class="org.liky.struts.action.ConvertListAction">
<result name="success">/output.jsp</result>
<result name="input">/converter.jsp</result>
</action>
<action name="register"
class="org.liky.struts.action.RegisterAction" method="register">
<interceptor-ref name="sampleInterceptorStack"></interceptor-ref>
<interceptor-ref name="authInterceptor"></interceptor-ref>
<interceptor-ref name="myMethodInterceptor">
<param name="excludeMethods">execute</param>
<param name="includeMethods">register</param>
</interceptor-ref>
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
</action>
<action name="fileUpload"
class="org.liky.struts.action.FileUploadAction">
<result name="success">/showUser.jsp</result>
<result name="input">/fileupload.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="fileUpload">
<!-- 如果需要制定单个上传文件的大小,和文件类型,请使用这两个参数来指定
<param name="maximumSize">102400</param>
<param name="allowedTypes">application/vnd.ms-word</param>
-->
</interceptor-ref>
</action>
</package>
</struts>