Struts2默认是使用了Common-FileUpload的文件上传框架,并对其进行封装,简化,使用时相当方便。
首先写一张简单的上传页面:
upload.html
<html>
<head>
<title>file upload</titel>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="upload.action">
file:<input type="file" name="upload"/><br>
<input name="dd" type="submit" value="submit"/>
</form>
</body>
</html>
配置处理的Action:
UploadAction.java
其中必须提供3个属性,
类型为File的xxx属性来封装文件信息
类型为String
的xxxFileName属性来封装文件域对应文件的文件名
类型为Stri
ng的xxxContentType属性来封装文件域中对应文件的文件类型
在下面的代码
中还定义了一个savePath属性,依赖注入方式,通过配置文件里来动态修改属性的值
package my;
import java.io.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
{
/**
*
*/
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
public void setSavePath(String value)
{
this.savePath = value;
}
private String getSavePath()
{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setUpload(File upload)
{
this.upload = upload;
}
public File getUpload()
{
return this.upload;
}
public void setUploadContentType(String uploadContentType)
{
this.uploadContentType = uploadContentType;
}
public String getUploadContentType()
{
return this.uploadContentType;
}
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;
}
}
配置Action:
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name ="my" extends="struts-default">
<action name="upload" class="my.UploadAction">
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
</action>
</package>
</struts>
配置web.xml文件:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
处理结果页面:succ.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>upload success</title>
</head>
<body>
<s:property value="title"/>
<br>
<img src="<s:property value="'upload/'+uploadFileName"/>"/> <br>
</body>
</html>
这样实现了简单的文件上传功能.
在实际应用时一般还需要文件过滤功能:
Struts2内置了一个文件上传拦截器fileUpload,通过配置此拦截器轻松实现文件过滤.
在配置过程时,可以指定两个参数:
alledTypes:指定允许上传的文件类型,多个 文件类型之间以英文逗号隔开
maximumSize:指定允许上传的文件大小。
当文件过滤失败后,系统自动转入input逻辑视图,所以在Action里需要配置input视图,另外还必须地为Action配置defaultStack的拦截器引用:
<?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 ="my" extends="struts-default">
<action name="upload" class="my.UploadAction">
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
<result name="input">/upload.jsp</result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/png,image/gif</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
</action>
</package>
</struts>
在input视图中通过<s:fielderror/>来显示错误信息,此信息是英文,配置国际化来显示中文信息(如下):
#更改上传文件类型不允许的提示信息
struts.messages.error.content.type.not.allowed=文件上传失败:你要上传的文件类型不允许
#更改上传文件太大的提示信息
struts.messages.error.file.too.large=文件上传失败:你要上传的文件太大
#文件上传其它错误信息
struts.messages.error.uploading=文件上传失败:发生内部错误