[标题]:[原]Struts2-文件上传与下载
[时间]:2009-8-7
[摘要]:Struts FileUpload & Download
[关键字]:浪曦视频,Struts2应用开发系列,WebWork,Apache,上传,下载,upload,download,commons
[环境]:struts-2.1.6、JDK6、MyEclipse7、Tomcat6
[作者]:Winty (wintys@gmail.com) http://www.blogjava.net/wintys
[正文]:
1、Apache Commons
Struts文件上传下载使用commons-fileupload与commons-io,struts-2.1.6中已有commons-fileupload-1.2.1.jar与commons-io-1.3.2.jar。其源代码下载地址分别为:
http://commons.apache.org/fileupload/
http://commons.apache.org/io/
2、文件上传
a.Servlet文件上传原理
此时,应将web.xml中的Struts filter注释掉,不然会导致错误。
/StrutsHelloWorld/WebRoot/upload/upload.jsp主要内容:
......
<form name="upload"
action="uploadServlet"
method="post"
enctype="multipart/form-data" >
用户名:<input type="text" name="user" /><br/>
附件:<input type="file" name="attachment"/><br/>
<input type="submit" name="submit" value="提交"/>
</form>
......
文件上传,需要设置method="post",enctype="multipart/form-data"。enctype默认值为:application/x-www-form-urlencoded。
服务器端接收,并将原始信息输出。
/StrutsHelloWorld/src/wintys/struts2/upload/UploadServlet.java:
package wintys.struts2.upload;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet文件上传原理示例
*
* @author Winty (wintys@gmail.com)
* @version 2009-8-1
* @see http://wintys.blogjava.net
*/
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = -8676112500347837364L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("UploadServlet...");
InputStream input = request.getInputStream();
BufferedInputStream bis = new BufferedInputStream(input);
String upfile = "c:/UploadedFile.txt";
OutputStream os = new FileOutputStream(upfile);
BufferedOutputStream bos = new BufferedOutputStream(os);
final int bytesReaded = 128;
byte[] buf = new byte[bytesReaded];
int len = -1;
while( (len = bis.read(buf, 0, bytesReaded)) != -1){
bos.write(buf, 0, len);
}
bis.close();
bos.close();
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
out.println("上传完成!存放于:" + upfile);
out.close();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req , resp);
}
}
在web.xml中的配置:
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>wintys.struts2.upload.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/upload/uploadServlet</url-pattern>
</servlet-mapping>
b.使用apache commons上传文件
/StrutsHelloWorld/WebRoot/upload/commonsUpload.jsp:
......
<form name="upload"
action="commonsUploadServlet"
method="post"
enctype="multipart/form-data">
用户名:<input type="text" name="user" /><br/>
附件:<input type="file" name="attachment"/><br/>
<input type="submit" name="submit" value="提交"/>
</form>
......
/StrutsHelloWorld/src/wintys/struts2/upload/CommonsUploadServlet.java:
package wintys.struts2.upload;
import java.io.*;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* 使用Apache Commons FileUpload 上传单个文件
*
* @author Winty (wintys@gmail.com)
* @version 2009-8-5
* @see http://wintys.blogjava.net
*/
@SuppressWarnings("serial")
public class CommonsUploadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request , response);
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//构造FileItemFactory工厂
int sizeThreshold = 1024;
File repository = new File("c:/upload/temp");
repository.mkdirs();
FileItemFactory factory = new
DiskFileItemFactory (sizeThreshold,repository);
//解析上传表单
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = null;
try {
items = upload.
parseRequest (request);
} catch (FileUploadException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
//分别处理简单表单和文件
for(FileItem item : items){
String name = item.getFieldName();
String value = null;
if(item.
isFormField() ){
//获取简单表单项的内容
value = item.getString("GBK");
}
else{
//获取上传文件的原始名
value = item.getName();
//如果有包含路径,则去掉
int slashIndex = -1;
if((slashIndex = value.lastIndexOf('/')) != -1
|| (slashIndex = value.lastIndexOf('""')) != -1){
value = value.substring(slashIndex + 1);
}
//写文件
File parent = new File("c:/upload");
parent.mkdirs();
File fileStore = new File(parent , value);
try {
item.
write (fileStore);
} catch (Exception e) {
e.printStackTrace();
}
}
request.setAttribute(name, value);
}
request.getRequestDispatcher("/upload/result.jsp")
.forward(request, response);
}
}
FileItem.isFormField()表示是否为file或简单表单项。
在web.xml中的配置:
<servlet>
<servlet-name>CommonsUploadServlet</servlet-name>
<servlet-class>wintys.struts2.upload.CommonsUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CommonsUploadServlet</servlet-name>
<url-pattern>/upload/commonsUploadServlet</url-pattern>
</servlet-mapping>
c.Struts文件上传
/StrutsHelloWorld/WebRoot/upload/strutsUpload.jsp :
......
<s:fielderror></s:fielderror>
<form name="upload"
action="strutsUpload"
method="post"
enctype="multipart/form-data">
用户名:<input type="text" name="user" /><br/>
附件:<input type="
file " name="
attachment "/><br/>
<input type="submit" name="submit" value="提交"/>
</form>
......
/StrutsHelloWorld/src/wintys/struts2/upload/UploadAction.java:
package wintys.struts2.upload;
import java.io.*;
import com.opensymphony.xwork2.ActionSupport;
/**
* 使用Struts上传单个文件
*
* @author Winty (wintys@gmail.com)
* @version 2009-8-5
* @see http://wintys.blogjava.net
*/
@SuppressWarnings("serial")
public class UploadAction extends ActionSupport {
String user;
File
attachment ;
String
attachment FileName;
String
attachment FileContentType;
//此处省略了getter and setter...
@Override
public String execute() throws Exception {
InputStream input = new FileInputStream(
attachment );
File parent = new File("c:/upload");
parent.mkdirs();
OutputStream output
= new FileOutputStream(new File(parent , attachmentFileName));
byte[] buf = new byte[1024*10];
int len = -1;
while((len = input.read(buf)) > 0){
output.write(buf, 0, len);
}
input.close();
output.close();
return SUCCESS;
}
@Override
public void validate() {
if(attachment == null){
addFieldError("attachment", "请选择上传的文件");
}
}
}
其中,"File attachment;"与form中的name相同<input type="file" name="attachment"/>。而"attachment",又决定了attachmentFileName、attachmentFileContentType的名称。
/StrutsHelloWorld/WebRoot/upload/strutsResult.jsp:
Result:<br/>
user: ${ requestScope.user } <br/>
attachment: <br/>
${ requestScope.
attachmentFileName }<BR/>
在struts.xml中的配置:
<action name="strutsUpload" class="wintys.struts2.upload.UploadAction">
<result name="success">/upload/strutsResult.jsp</result>
<result name="input">/upload/strutsUpload.jsp</result>
</action>
d.上传固定数量的文件
/StrutsHelloWorld/WebRoot/upload/strutsMultiFileUpload.jsp:
......
<s:fielderror></s:fielderror>
<form name="upload"
action="strutsMultiFileUpload"
method="post"
enctype="multipart/form-data">
用户名:<input type="text" name="user" /><br/>
附件1:<input type="file" name="attachment"/><br/>
附件2:<input type="file" name="attachment"/><br/>
附件3:<input type="file" name="attachment"/><br/>
<input type="submit" name="submit" value="提交"/>
</form>
......
/StrutsHelloWorld/src/wintys/struts2/upload/MultiFileUploadAction.java :
package wintys.struts2.upload;
import java.io.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* @author Winty (wintys@gmail.com)
* @version 2009-8-6
* @see http://wintys.blogjava.net
*/
@SuppressWarnings("serial")
public class MultiFileUploadAction extends ActionSupport {
String user;
List<File> attachment;
List<String> attachmentFileName;
List<String> attachmentFileContentType;
//此处省略了getter and setter...
@Override
public String execute() throws Exception {
for(int i = 0 ; i < attachment.size(); i++){
File attach = attachment.get(i);
InputStream input = new FileInputStream(attach);
File parent = new File("c:/upload");
parent.mkdirs();
File out = new File(parent , attachmentFileName.get(i));
OutputStream output = new FileOutputStream(out);
byte[] buf = new byte[1024];
int len = -1;
while((len = input.read(buf)) > 0){
output.write(buf, 0, len);
}
input.close();
output.close();
}
return SUCCESS;
}
@Override
public void validate() {
if(attachment == null){
addFieldError("attachment", "请选择文件");
}
}
/**
* 当上传文件大小大于struts.multipart.maxSize提示时,
* 客户端会出现如下错误:
* "the request was rejected because its size (4501994)
* exceeds the configured maximum (2097152)"。
* 此信息在commons-fileupload.jar,
* org.apache.commons.fileupload.FileUploadBase源代码中第904行。
*
* 重写addActionError()以替换默认信息。
*/
@Override
public void addActionError(String anErrorMessage) {
//这里要先判断一下,是我们要替换的错误,才处理
if (anErrorMessage.startsWith("the request was rejected because its size")) {
//这些只是将原信息中的文件大小提取出来。
Matcher m = Pattern.compile("""d+").matcher(anErrorMessage);
String s1 = "";
if (m.find()) s1 = m.group();
String s2 = "";
if (m.find()) s2 = m.group();
//偷梁换柱,将信息替换掉
super.addActionError("你上传的文件(" + s1 + ")超过允许的大小(" + s2 + ")");
} else {//不是则不管它
super.addActionError(anErrorMessage);
}
}
}
在web.xml中的配置:
<action name="strutsMultiFileUpload" class="wintys.struts2.upload.MultiFileUploadAction">
<result name="success">/upload/strutsResult.jsp</result>
<result name="input">/upload/strutsMultiFileUpload.jsp</result>
</action>
e.上传任意个数文件
wintys.struts2.upload.MultiFileUploadAction不需要改变,只需要修改strutsMultiFileUpload.jsp为
/StrutsHelloWorld/WebRoot/upload/strutsMultiUpload.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>upload</title>
</head>
<style type="text/css">
.attachLine{
border: 1px solid #00C0EE;
margin-bottom:10px;
}
.desc{
display:inline;
color:blue;
}
.attach{
display:inline;
border:1px solid #8800AA;
margin-left:30px;
}
.remove{
display: inline;
margin-left:30px;
}
</style>
<script type="text/javascript">
var attachmentCounts = 1;
function addMore(){
if(attachmentCounts >= 5){
window.alert("附件数不能大于" + 5);
return;
}
attachmentCounts++;
var attachment = document.getElementById("attachment");
var line = document.createElement("div");
line.setAttribute("class" , "attachLine");
line.setAttribute("className" , "attachLine");//for IE
//标签
var descDiv = document.createElement("div");
descDiv.setAttribute("class" , "desc");
descDiv.setAttribute("
className " , "desc");//for IE
var desc = document.createTextNode("附件:");
descDiv.appendChild(desc);
line.appendChild(descDiv);
//文件上传
var attachDiv = document.createElement("div");
attachDiv.setAttribute("class" , "attach");
attachDiv.setAttribute("className" , "attach");//for IE
var attach = document.createElement("input");
attach.setAttribute("type" , "file");
attach.setAttribute("name" , "attachment");
attachDiv.appendChild(attach);
line.appendChild(attachDiv);
//移除
var removeDiv = document.createElement("div");
removeDiv.setAttribute("class" , "remove");
removeDiv.setAttribute("className" , "remove");//for IE
var remove = document.createElement("input");
remove.setAttribute("type" , "button");
remove.setAttribute("value" , "移除");
remove.onclick = function removeLine(){
attachmentCounts--;
attachment.removeChild(line);
}
removeDiv.appendChild(remove);
line.appendChild(removeDiv);
attachment.appendChild(line);
}
</script>
<body>
<s:actionerror/>
<s:fielderror></s:fielderror>
<form name="upload"
action="strutsMultiUpload"
method="post"
enctype="multipart/form-data">
用户名:<input type="text" name="user" /><br/>
<div id="attachment" >
<div>
<input type="button" value="增加附件个数" onclick="
addMore() ;"/>
</div>
<div class="attachLine">
<div class="desc">附件:</div>
<div class="attach"><input type="file" name="attachment"/></div>
</div>
</div>
<input type="submit" name="submit" value="提交" />
</form>
</body>
</html>
在struts.xml中的配置:
<action name="strutsMultiUpload" class="wintys.struts2.upload.MultiFileUploadAction">
<result name="success">/upload/strutsResult.jsp</result>
<result name="input">/upload/strutsMultiUpload.jsp</result>
</action>
f.文件上传相关配置
给文件上传拦截器fileUpload配置参数,在struts.xml中:
......
<interceptor name="myUploadInterceptor" class="wintys.struts2.upload.MyFileUploadInterceptor" />
<interceptor-stack name="myUploadStack">
<interceptor-ref name="
fileUpload ">
<param name="maximumSize">204800</param>
<param name="allowedTypes">
image/jpeg,image/pjpeg,text/plain,application/vnd.ms-powerpoint
</param>
<param name="allowedExtensions">txt,jpg</param>
</interceptor-ref>
<interceptor-ref name="
myUploadInterceptor " />
<interceptor-ref name="basicStack"/>
<interceptor-ref name="validation"/>
<interceptor-ref name="workflow"/>
</interceptor-stack>
......
<action name="strutsMultiUpload" class="wintys.struts2.upload.MultiFileUploadAction">
<result name="success">/upload/strutsResult.jsp</result>
<result name="input">/upload/strutsMultiUpload.jsp</result>
<interceptor-ref name="myUploadStack"></interceptor-ref>
</action>
说明:
a).allowedTypes:可以从tomcat/conf/web.xml中查得minetype。
b).myUploadInterceptor: 自定义拦截器。对Struts fileUpload进行拦截,如果上传文件中出现错误,就直接返回到Action.INPUT;如果没有错误,则继续执行。
c).validation: 进行校验。
d).workflow: 保证validation中添加错误后,不再执行execute()。
如果不加myUploadInterceptor,会出现上传过大文件,给客户端提示出错信息后,还继续执行validate()方法。参照FileUploadInterceptor源代码,得到以下解决方案。
myUploadInterceptor=>wintys.struts2.upload.MyFileUploadInterceptor :
/StrutsHelloWorld/src/wintys/struts2/upload/MyFileUploadInterceptor.java :
package wintys.struts2.upload;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ValidationAware;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* 用于对Struts默认文件上传拦截器(FileUploadInterceptor)进行处理,
* 如果上传文件中出现错误,就直接返回到Action.INPUT;
* 没有错误,则继续执行。
*
* 配置如下:
* <interceptor name="myUploadInterceptor" class="wintys.struts2.upload.MyFileUploadInterceptor" />
* <interceptor-stack name="myUploadStack">
* <interceptor-ref name="fileUpload">
* <param name="maximumSize">204800</param>
* <param name="allowedTypes">
* image/jpeg,image/pjpeg,text/plain,application/vnd.ms-powerpoint
* </param>
* <param name="allowedExtensions">txt,jpg</param>
* </interceptor-ref>
* <interceptor-ref name="myUploadInterceptor" />
* <interceptor-ref name="basicStack"/>
* <interceptor-ref name="validation"/>
* <interceptor-ref name="workflow"/>
* </interceptor-stack>
* @author Winty (wintys@gmail.com)
* @version 2009-8-7
* @see http://wintys.blogjava.net
*/
@SuppressWarnings("serial")
public class MyFileUploadInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ValidationAware validation = null;
Object action = invocation.getAction();
if (action instanceof ValidationAware) {
validation = (ValidationAware) action;
}
//如果上传文件中出现错误,就直接返回到INPUT;没有错误,则继续执行
String result = Action.INPUT;
if(validation == null || !validation.hasErrors())
result = invocation.invoke();
return result;
}
}
struts2-core-2.1.6.jar/org.apache.struts2/struts-message.properties定义了原始的提示信息。重定义提示信息,新建/StrutsHelloWorld/src/message.properties(与struts.xml相同目录):
#file upload 需要转换成unicode
struts.messages.error.file.too.large=文件过大:{1}
struts.messages.error.content.type.not.allowed=文件类型不允许:{1}
struts.messages.error.uploading=文件上传失败:发生内部错误
struts2-core-2.1.6.jar/org.apache.struts2/default.properties中定义了默认配置。自己改变配置默认(编码等),在struts.xml中:
......
<!-- 上传文件临时存放地 -->
<constant name="struts.multipart.saveDir" value="c:/upload/temp"></constant>
<!-- 默认编码:不配置中文会出现乱码 -->
<constant name="struts.i18n.encoding" value="GBK"></constant>
<!-- 总的文件大小:about 2M -->
<constant name="
struts.multipart.maxSize " value="2097152"></constant>
......
g.注意问题
文件过大的提示信息:
当文件大小<maximumSize,正常上传。
当maximumSize < 文件大小 < struts.multipart.maxSize提示struts.messages.error.file.too.large。
当文件大小>struts.multipart.maxSize提示:
"the request was rejected because its size (4501994) exceeds the configured maximum (2097152) "。
"the request was rejected..."这个信息是写死在代码中的(commons-fileupload-1.2.1.jar/org.apache.commons.fileupload.FileUploadBase 第902行),没有做国际化,所以必须另外对待。直接修改FileUploadBase代码中的字符串,或override com.opensymphony.xwork2.ActionSupport.addActionError()。override addActionError()代码见/StrutsHelloWorld/src/wintys/struts2/upload/MultiFileUploadAction.java 。
3、文件下载
/StrutsHelloWorld/WebRoot/upload/fileDownload.jsp:
......
<s:a href="strutsFileDownload.action" >点击这里下载文件</s:a>
......
/StrutsHelloWorld/src/wintys/struts2/upload/DownloadAction.java
package wintys.struts2.upload;
import java.io.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class DownloadAction extends ActionSupport {
public InputStream getDownloadFile() {
String filepath = "/upload/fordownloadtest.ppt";
InputStream is =
ServletActionContext .getServletContext()
.getResourceAsStream(filepath);
return is;
}
/**
* struts.xml中配置如下:
* <param name="contentDisposition">
* attachment;filename="${fileName}"
* </param>
*
* ${fileName}:表明需要在DownloadAction中提供getFileName()方法。
*
* @return 下载文件的文件名
*/
public String getFileName(){
String filename = "Test下载测试文件.ppt";
String fn = "test.ppt";
try {
fn =new String(filename.getBytes() , "
ISO8859-1 ");
} catch (UnsupportedEncodingException e) {
fn = "error.ppt";
e.printStackTrace();
}
return fn;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
在struts.xml中的配置:
<action name="strutsFileDownload" class="wintys.struts2.upload.DownloadAction">
<result name="success" type="stream"><!--type为stream,使用StreamResult处理-->
<param name="contentType">application/octet-stream</param>
<param name="inputName">downloadFile</param>
<param name="contentDisposition">attachment;filename="
${fileName} "</param>
</result>
</action>
说明:
a).inputName=downloadFile,指明DownloadAction中public InputStream getDownloadFile()返回文件流。
b).type="stream",说明使用org.apache.dispatcher.StreamResult处理这个result。
c).contentDisposition、contentType等属性可以在<param>里配置,也可以在DownloadAction中直接提供getter。
[参考资料]:
[1] 《浪曦视频之Struts2应用开发系列》
[2] 谈谈我对Struts2文件上传的理解 : http://www.iteye.com/topic/287800
[3] Struts2中文件上传错误提示信息the request was rejected because its size的解决办法 : http://www.blogjava.net/cmzy/archive/2009/04/15/265703.html
[4] 理解和灵活应用 Struts2 的文件下载功能 : http://www.blogjava.net/Unmi/archive/2009/06/17/282780.html
[附件]:
源代码 : Struts_FileUpload_Download.zip