二、 Struts 2 的文件上传
Struts 2 并未提供自己的请求解析器,它需要调用其他上传框架来解析二进制请求数据。但 Struts 2 在原有的上传解析器基础上做了进一步封装,简化了文件上传
2.1 Struts 2的文件上传支持
在 Struts 2 的 struts.properties 配置文件中,可以看到上传文件解析器
# 指定使用 COS 的文件上传解析器 # struts.multipart.parser=cos # 指定使用 Pell 的文件上传解析器 # struts.multipart.parser=pell # struts 2 默认使用 Jakarta 的 Common-FileUpload 的文件上传解析器 struts.multipart.parser=jakarta
无论用哪一种上传解析器,struts 2 的封装都取消了它们的区别,因此开发者可以轻松切换上传解析器
struts 2 默认使用 Common-FileUpload ,因此 需要加入,commons-io-1.3.2.jar 和 commons-fileupload-1.2.1.jar 。
2.2 实现文件上传的 Action
upload.html
<form action="upload.action" method="post"
enctype="multipart/form-data">
文件标题:<input type="text" name="title" /><br />
选择文件:<input type="file" name="upload" /><br />
<input value="上传" type="submit" />
</form>
UploadAction.java
import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.*;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
{
//封装文件标题请求参数的属性
private String title;
//封装上传文件域的属性
private File upload;
//封装上传文件类型的属性
private String uploadContentType;
//封装上传文件名的属性
private String uploadFileName;
//直接在struts.xml文件中配置的属性
private String savePath;
//接受struts.xml文件配置值的方法
public void setSavePath(String value)
{
this.savePath = value;
}
//返回上传文件的保存位置
private String getSavePath() throws Exception
{
return ServletActionContext.getRequest().getRealPath(savePath);
}
//文件标题的setter和getter方法
public void setTitle(String title)
{
this.title = title;
}
public String getTitle()
{
return (this.title);
}
//上传文件对应文件内容的setter和getter方法
public void setUpload(File upload)
{
this.upload = upload;
}
public File getUpload()
{
return (this.upload);
}
//上传文件的文件类型的setter和getter方法
public void setUploadContentType(String uploadContentType)
{
this.uploadContentType = uploadContentType;
}
public String getUploadContentType()
{
return (this.uploadContentType);
}
//上传文件的文件名的setter和getter方法
public void setUploadFileName(String uploadFileName)
{
this.uploadFileName = uploadFileName;
}
public String getUploadFileName()
{
return (this.uploadFileName);
}
@Override
public String execute() throws Exception
{
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath()
+ "\\" + getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
return SUCCESS;
}
}
xxx, xxxFileName ,xxxContentType ,这个3个属性,可以更简单地实现文件上传。
savePath 属性 ,通过配置文件来设置,从而允许动态设置该属性值(依赖注入)
提示: Struts 2 的 Action 中的属性,功能非常丰富,除了可以用于封装 HTTP 请求参数外,也可以封装 Action 的处理结果。不仅如此,Action 属性还可以通过在 Struts 2 配置文件中进行配置,接收 Struts 2 框架的注入,允许在配置文件中为该属性动态指定值。
2.3 配置文件上传的 Action
struts.xml
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- 指定国际化资源文件的baseName为globalMessages --> <constant name="struts.custom.i18n.resources" value="globalMessages"/> <!-- 设置该应用使用的解码集 --> <constant name="struts.i18n.encoding" value="GBK"/> <package name="lee" extends="struts-default"> <!-- 配置处理文件上传的Action --> <action name="upload" class="lee.UploadAction"> <!-- 动态设置Action的属性值 --> <param name="savePath">/upload</param> <!-- 配置Struts 2默认的视图页面 --> <result>/succ.jsp</result> </action> <action name=""> <result>.</result> </action> </package> </struts>
succ.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>上传成功</title>
</head>
<body>
上传成功!<br />
文件标题:<s:property value=" + title"/><br />
文件为:<img src="<s:property
value="'upload/' + uploadFileName"/>"/><br />
</body>
</html>
以上2段代码与 2.2 中一起使用
2.4 手动实现文件过滤
限制浏览者自由上传各种文件,或限制上传文件大小等等,可以使用文件过滤
手动实现文件过滤的步骤:
① 在 Action 中定义一个专门用于进行文件过滤的方法,方法名是任意的,该方法的逻辑就是判断上传文件的类型是否为允许的类型 ,如下 filterType() 方法
import com.opensymphony.xwork2.*;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.*;
public class UploadAction extends ActionSupport
{
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
//接受依赖注入的属性
private String savePath;
//接受依赖注入的方法
public void setSavePath(String value)
{
this.savePath = value;
}
private String getSavePath() throws Exception
{
return ServletActionContext.getRequest().getRealPath(savePath);
}
private String allowTypes;
//allowTypes属性的setter和getter方法
public String getAllowTypes()
{
return allowTypes;
}
public void setAllowTypes(String allowTypes)
{
this.allowTypes = allowTypes;
}
public void setTitle(String title) {
this.title = title;
}
public void setUpload(File upload) {
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getTitle() {
return (this.title);
}
public File getUpload() {
return (this.upload);
}
public String getUploadContentType() {
return (this.uploadContentType);
}
public String getUploadFileName() {
return (this.uploadFileName);
}
/**
* 过滤文件类型
* @param types 系统所有允许上传的文件类型
* @return 如果上传文件的文件类型允许上传,返回null,否则返回input字符串
*/
public String filterType(String[] types)
{
//获取希望上传的文件类型
String fileType = getUploadContentType();
for (String type : types)
{
if (type.equals(fileType))
{
return null;
}
}
return INPUT;
}
@Override
public String execute() throws Exception
{
//将允许上传文件类型的字符串以英文逗号(,)分解成字符串数组
//从而判断当前文件类型是否允许上传
String filterResult = filterType(getAllowTypes().split(","));
//如果当前文件类型不允许上传
if (filterResult != null)
{
ActionContext.getContext().put("typeError","您要上传的文件类型不正确!");
return filterResult;
}
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" +
getUploadFileName());
//以上传文件建立一个文件上传流
FileInputStream fis = new FileInputStream(getUpload());
//将上传文件的内容写入服务器
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
return SUCCESS;
}
}
为了让文件上传类型校验失败时能返回 input 逻辑视图,在 struts.xml 中加入 input :
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- 指定国际化资源文件的baseName为globalMessages --> <constant name="struts.custom.i18n.resources" value="globalMessages"/> <!-- 设置该应用使用的解码集 --> <constant name="struts.i18n.encoding" value="GBK"/> <package name="lee" extends="struts-default"> <!-- 配置处理文件上传的Action --> <action name="upload" class="lee.UploadAction"> <!-- 设置允许上传的文件类型 --> <param name="allowTypes">image/bmp, image/png,image/gif,image/jpeg</param> <!-- 动态设置Action的属性值 --> <param name="savePath">/upload</param> <!-- 上传失败后退回input逻辑视图 --> <result name="input">/upload.jsp</result> <!-- 配置Struts 2默认的视图页面 --> <result>/succ.jsp</result> </action> <action name=""> <result>.</result> </action> </package> </struts>
upload.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>通过代码实现文件类型过滤</title>
<meta name="website" content="http://www.crazyit.org" />
</head>
<body>
<span style="color:red">${requestScope.typeError}</span>
<form action="upload.action" method="post"
enctype="multipart/form-data">
文件标题:<input type="text" name="title" /><br />
选择文件:<input type="file" name="upload" /><br />
<input value="上传" type="submit" />
</form>
</body>
</html>
错误提示 <span style="color:red">${requestScope.typeError}</span>
提示: 如果需要实现文件大小过滤,可以调用 File 类 的 length() 方法来获取上传文件大小。除此之外,Struts 2 的 struts.multipart.maxSize 常量也用于限制允许上传的文件大小,这个常量对于整个 Struts 2 应用都会起作用
2.5 拦截器实现文件过滤
Struts 2 提供一个文件上传的拦截器,通过配置拦截器可以更轻松实现文件过滤。Struts 2 中文件上传的过滤器是 fileUpload 为了让该拦截器起作用,只需要在该 Action 中配置该拦截器引用即可。
配置 fileUpload 拦截器时,可以指定 2 个参数 :
● allowedTypes : 指定允许上传的文件类型,多个文件类型之间用英文逗号(,)隔开
● maximumSize : 指定允许上传的文件大小,单位是字节
通过配置 fileUpload 拦截器,可以轻松地实现文件过滤,过滤失败后,自动转入 input 逻辑视图
在 Action 配置 input,还有 defaultStack 的拦截器引用 :
struts.xml
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- 指定国际化资源文件的baseName为globalMessages --> <constant name="struts.custom.i18n.resources" value="globalMessages"/> <!-- 设置该应用使用的解码集 --> <constant name="struts.i18n.encoding" value="GBK"/> <package name="lee" extends="struts-default"> <!-- 配置处理文件上传的Action --> <action name="upload" class="lee.UploadAction"> <!-- 配置fileUpload的拦截器 --> <interceptor-ref name="fileUpload"> <!-- 配置允许上传的文件类型 --> <param name="allowedTypes">image/bmp,image/png, image/gif,image/jpeg</param> <!-- 配置允许上传的文件大小 --> <param name="maximumSize">2000</param> </interceptor-ref> <!-- 配置系统默认的拦截器 --> <interceptor-ref name="defaultStack"/> <!-- 动态设置Action的属性值 --> <param name="savePath">/upload</param> <!-- 配置input逻辑视图对应的视图页面 --> <result name="input">/upload.jsp</result> <!-- 配置Struts 2默认的视图页面 --> <result>/succ.jsp</result> </action> <action name=""> <result>.</result> </action> </package> </struts>
提示: 如果需要使用文件上传拦截器来过滤文件大小,或者过滤文件内容,则必须显式配置引用 Struts 2 默认的拦截器栈:defaultStack
2.6 输出错误提示
如果上传失败,系统将返回 input 逻辑视图,但是没有任何提示。
为了在上传页面中输出失败提示,需要使用struts 2 的标签库,在 input 逻辑视图页面增加如下:
<!-- 输出错误提示 -->
<span style="color:red" ><s:fielderror/></span>
上面的代码将会把文件过滤失败的信息显示在该页面上,给用户重新上传生成提示。
国际化
默认是英文提示,需要国际化
● 上传文件太大的提示信息的 key 是 "struts.messages.error.file.too.large" , 如果在自己的国际化资源文件中增加该 key 的消息,将可改变该提示信息。
● 不允许上传的文件类型的提示信息的 key 是 "struts.messages.error.content.type.not.allowed" 的信息;如果在自己国际化资源文件中增加该 key 的消息,将可改变文件类型不允许的提示信息
● 文件上传的未知错误的key 为 "struts.messages.error.uploading" 的信息,如果用户上传文件失败,既不是文件类型不允许,也不是文件大小超出允许大小,则系统将提示页面输出该 key 的消息
globalMessages.properties
#改变文件类型不允许的提示信息 struts.messages.error.content.type.not.allowed=您上传的文件类型只能是图片文件!请重新选择! #改变上传文件太大的提示信息 struts.messages.error.file.too.large=您要上传的文件太大,请重新选择! #未知错误的提示信息 struts.messages.error.uploading=文件上传出现未知错误,请重试!
upload.jsp
<body>
<!-- 输出错误提示 -->
<span style="color:red"><s:fielderror/></span>
<form action="upload.action" method="post"
enctype="multipart/form-data">
文件标题:<input type="text" name="title" /><br />
选择文件:<input type="file" name="upload" /><br />
<input value="上传" type="submit" />
</form>
</body>
2.7 文件上传的常量配置
当每次上传文件时,都可以在 Tomcat 的控制台看见如下信息:
INFO (org.apache.struts2.dispatcher.Dispatcher:624) - Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir INFO (org.apache.struts2.interceptor.FileUploadInterceptor:277) - Removing file upload D:\tomcat\work\Catalina\localhost\simpleUpload\upload_103b2706_112b45dc4a3_8000_00000001.tmp
第一个提示信息 : 系统找不到 struts.multipart.saveDiv 常量的设置,默认使用 javax.servlet.context.tempdir 路径,这是因为 Struts 2 执行文件上传过程中,需要指定一个临时文件夹,如果没有指定临时文件夹,系统默认使用 javax.servlet.context.tempdir, 在 Tomcat 安装路径下的 work\Catalina\localhost\路径下
第二个提示信息 : 系统正在删除一个临时文件,该临时文件就是上传过程中产生的临时文件
如果为了避免文件上传时使用 Tomcat 的工作路径作为临时路径,则应该设置 struts.multipart.saveDir 属性。设置该常量既可以通过 struts.properties 配置 , 也可以通过 struts.xml 配置
除此之外,还有一个文件上传的常量: struts.multipart.maxSize ,该常量设置整个表单请求内容的最大字节数。它可以控制整个 Struts 2 应用里上传文件的总大小不能超过该值。
2.8 使用 Pell 上传
Struts 2.0 ,2.1 都不支持 COS
为了在 Struts 2 中使用 Pell 上传, 把 struts2-pell-multipart-plugin-2.1.6.jar 文件复制到 Web 应该的 WEB-INF\lib 下。
打开 struts2-pell-multipart-plugin-2.1.6.jar ,里面有 PellMultiPartRequest 类。还有 struts-plugin.xml 文件----这是 Struts 2 插件的配置文件,Struts 2 应用会自动加载该配置文件。该文件只有一个 bean 配置,如下:
<bean class="org.apache.struts2.dispatcher.multipart.PellMultiPartRequest" name="pell" type="org.apache.struts2.dispatcher.multipart.MultiPartRequest"/>
struts.xml
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- 指定国际化资源文件的baseName为globalMessages --> <constant name="struts.custom.i18n.resources" value="globalMessages"/> <!-- 指定使用Pell文件上传机制 --> <constant name="struts.multipart.parser" value="pell"/> <!-- 设置该应用使用的解码集 --> <constant name="struts.i18n.encoding" value="GBK"/> <package name="lee" extends="struts-default"> <!-- 配置处理文件上传的Action --> <action name="upload" class="lee.UploadAction"> <!-- 配置fileUpload的拦截器 --> <interceptor-ref name="fileUpload"> <!-- 配置允许上传的文件类型 --> <param name="allowedTypes">image/bmp,image/png, image/gif,image/jpeg</param> <!-- 配置允许上传的文件大小 --> <param name="maximumSize">2000000</param> </interceptor-ref> <!-- 配置系统默认的拦截器 --> <interceptor-ref name="defaultStack"/> <!-- 动态设置Action的属性值 --> <param name="savePath">/upload</param> <!-- 配置input逻辑视图对应的视图页面 --> <result name="input">/upload.jsp</result> <!-- 配置Struts 2默认的视图页面 --> <result>/succ.jsp</result> </action> <action name=""> <result>.</result> </action> </package> </struts>
除此之外,我们还应该将 Pell 的核心 JAR 包复制过来,multipartrequest.jar
至于文件上传的 Action ,配置 Action , 过滤文件类型等,与使用 Common-FileUpload 进行文件上传完全相同