本系列博客汇总在这里:Struts2 汇总
一、单文件上传源码工程文件为:struts2_15
上传文件对表单的要求:
- 表单要使用 post 方式提交;
- 表单的 enctype 是 multipart/form-data;
- 表单中要有 file 类型的 input 文本域。
Struts上传也是基于拦截器,底层还是使用 commons-fileupload 组件,Struts上传的步骤如下:
- 建立表单
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:fielderror></s:fielderror> <s:actionerror/> <form action="${pageContext.request.contextPath }/upload/weiyuxuan" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="username"><br> 文件:<input type="file" name="upload"><br> <input type="submit"> </form> </body> </html>
- 创建 Action
注意:
接收的文件属性:和表单中文本域 file 类型 input 的 name 一致!
接收的文件名字:file 的 name+FileName 固定写法!
接收的文件 MIME 类型:file 的 name+ContentType。
配置package com.wyx.action; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.ServletContext; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /** * action动作类充当模型对象 */ public class UploadAction extends ActionSupport { private String username; /** * 要接收的文件,命名需要和表单中的file类型的input的name一致 */ private File upload; /** * 文件名的接收 File属性名FileName:固定写法 */ private String uploadFileName; /** * 获得上传文件的MIME类型,File属性名字ContentType:固定写法 */ private String uploadContentType; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } /*public String upload() throws Exception { //获得servletContext ServletContext sc = ServletActionContext.getServletContext(); //获得服务的绝对路径 String realPath = sc.getRealPath("/"); realPath = realPath + "upload\\"+uploadFileName; //定义输入输出流 InputStream in = new FileInputStream(upload); OutputStream out = new FileOutputStream(realPath); int len = -1; byte[] bs = new byte[1024]; while((len = in.read(bs)) != -1) { out.write(bs, 0, len); } out.close(); in.close(); return super.SUCCESS; }*/ public String upload() throws Exception { // 获得servletContext ServletContext sc = ServletActionContext.getServletContext(); // 获得服务的绝对路径 String realPath = sc.getRealPath("/"); realPath = realPath + "upload\\" + uploadFileName; // 定义输入输出流 FileUtils.copyFile(upload, new File(realPath)); return super.SUCCESS; } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 开发模式 --> <constant name="struts.devMode" value="true"></constant> <package name="upload" extends="struts-default" namespace="/upload"> <!-- 拦截器配置 --> <action name="weiyuxuan" class="com.wyx.action.UploadAction" method="upload"> <result name="success">/success.jsp</result> <result name="input">/form.jsp</result> </action> </package> </struts>
验证文件的类型:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 开发模式 -->
<constant name="struts.devMode" value="true"></constant>
<package name="upload" extends="struts-default" namespace="/upload">
<!-- 拦截器配置 -->
<action name="weiyuxuan" class="com.wyx.action.UploadAction" method="upload">
<!-- 主动引用默认拦截器栈 -->
<interceptor-ref name="defaultStack">
<!--设置上传拦截器fileUpload.allowedExtensions, 不要使用allowedExtensionsSet-->
<param name="fileUpload.allowedExtensions">.png,.txt</param>
</interceptor-ref>
<result name="success">/success.jsp</result>
<result name="input">/form.jsp</result>
</action>
</package>
</struts>
验证文件的大小:
- Struts2 默认的上传文件的配置在 default.properties 的文件中,由 struts.multipart.maxSize=2097152 控制。
- 文件超过最大值的提示信息是在 struts_message.propertise 中,我们只要通过国际化文件覆盖它即可。
<!-- 设置文件上传的大小,以字节为单位 --> <constant name="struts.multipart.maxSize" value="1048576"></constant>
我们发现展示并不是中文,我们使用国际化来处理,创建国际化文件:
在国际化文件中 {0} 是去文件最大值,{1} 取我们上次的文件的大小。然后再 struts.xml 中加载资源文件:<!-- 国际化 --> <constant name="struts.custom.i18n.resources" value="com/wyx/action/resource/msg"></constant>
多文件上传 Action 中把 file 属性以及和 file 属性相关的上传文件都变成数组,在 execute 中循环上传即可。
package com.wyx.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* action动作类充当模型对象
*/
public class UploadsAction extends ActionSupport
{
private String username;
/**
* 要接收的文件,命名需要和表单中的file类型的input的name一致
*/
private File[] upload;
/**
* 文件名的接收 File属性名FileName:固定写法
*/
private String[] uploadFileName;
/**
* 获得上传文件的MIME类型,File属性名字ContentType:固定写法
*/
private String[] uploadContentType;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public File[] getUpload()
{
return upload;
}
public void setUpload(File[] upload)
{
this.upload = upload;
}
public String[] getUploadFileName()
{
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName)
{
this.uploadFileName = uploadFileName;
}
public String[] getUploadContentType()
{
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType)
{
this.uploadContentType = uploadContentType;
}
public String upload() throws Exception
{
// 获得servletContext
ServletContext sc = ServletActionContext.getServletContext();
// 获得服务的绝对路径
for (int i = 0; i < upload.length; i++)
{
String realPath = sc.getRealPath("/");
realPath = realPath + "upload\\" + uploadFileName[i];
// 定义输入输出流
FileUtils.copyFile(upload[i], new File(realPath));
}
return super.SUCCESS;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:fielderror></s:fielderror>
<s:actionerror/>
<form action="${pageContext.request.contextPath }/upload/weiyuxuans" method="post" enctype="multipart/form-data">
姓名:<input type="text" name="username"><br>
文件1:<input type="file" name="upload"><br>
文件2:<input type="file" name="upload"><br>
文件3:<input type="file" name="upload"><br>
<input type="submit">
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 开发模式 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 国际化 -->
<constant name="struts.custom.i18n.resources" value="com/wyx/action/resource/msg"></constant>
<!-- 设置文件上传的大小,以字节为单位 -->
<constant name="struts.multipart.maxSize" value="1048576"></constant>
<package name="upload" extends="struts-default" namespace="/upload">
<!-- 多文件上传 -->
<action name="weiyuxuans" class="com.wyx.action.UploadsAction" method="upload">
<!-- 主动引用默认拦截器栈 -->
<interceptor-ref name="defaultStack">
<!-- 设置上传拦截器fileUpload.allowedExtensions, 不要使用allowedExtensionsSet -->
<param name="fileUpload.allowedExtensions">.png,.txt</param>
</interceptor-ref>
<result name="success">/success.jsp</result>
<result name="input">/form1.jsp</result>
</action>
</package>
</struts>
如有错误,欢迎指正!