先来看下效果图:
输入界面
结果页面
输入界面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- 导入struts标签库 -->
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>多文件上传</h2>
<form action="uploadManyAction!upload.action" method="post" enctype="multipart/form-data">
文件标题:<input type="text" name="title"><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" value="提交">
</form>
</body>
</html>
action部分代码:
package com.hcj.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadManyAction extends ActionSupport {
//封装文件标题请求参数的属性
private String title;
//封装上传文件域的属性(属性名和前台input里name值相同)
private List<File> upload;
//封装上传文件类型的属性(属性名称为前台name值+ContentType,如此处前台name值为upload,因此此处为uploadContentType)
private List<String> uploadContentType;
//封装上传文件名的属性(属性名称为前台name值+FileName,如此处前台name值为upload,因此此处为uploadFileName)
private List<String> uploadFileName;
//配置保存路径(直接在struts.xml文件中配置)
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<File> getUpload() {
return upload;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public List<String> getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(List<String> uploadContentType) {
this.uploadContentType = uploadContentType;
}
public List<String> getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(List<String> uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath() {
/**
* 注意事项
* 1.并不是和其他方法一样,简单的返回savePath
* 2.可能有些书上这里返回服务器的路径用的是ServletActionContext.getRequset().getRealPath(savePath),
* 但是这个方法已经过期,现用ServletActionContext.getServletContext().getRealPath代替该方法
* 3.如果你第二步方法报The method getServletContext() from the type ServletActionContext refers to the missing type ServletContext错误,
* 则是因为少了servlet-api.jar的包,该包在你tomcat目录下的lib文件里可找到,将其导入你WEB-INF下的lib包里即可。
*/
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String upload() throws Exception {
//取得需要上传的文件数组
List<File> files = getUpload();
//遍历每个需要上传的文件
for (int i = 0; i < files.size(); i++) {
//以服务器的文件保存地址和原文件名建立文件上传输出流
File outFile = new File(getSavePath() + "\\" + getUploadFileName().get(i));
//创建字节输出流
FileOutputStream fos = new FileOutputStream(outFile);
//创建字节输入流
FileInputStream fis = new FileInputStream(getUpload().get(i));
//设置缓冲区
byte[] buffer = new byte[1024];
int len=0;
//读取上传的文件
while((len = fis.read(buffer))>0){
//写入目标文件
fos.write(buffer, 0, len);
}
//关流
fos.close();
fis.close();
}
return SUCCESS;
}
}
struts.xml部分代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<!-- Struts2配置文件的根元素 -->
<struts>
<!-- 配置了系列常量 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 开启动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- 指定国际化资源 -->
<constant name="struts.custom.i18n.resources" value="globalMessages"></constant>
<!-- 配置项目所上传文件的最大的Size -->
<constant name="struts.multipart.maxSize" value="9000000"/>
<package name="default" extends="struts-default" namespace="">
<default-action-ref name="default"></default-action-ref>
<action name="default">
<result name="success">/success</result>
</action>
<!-- 多文件上传 -->
<action name="uploadManyAction" class="com.hcj.action.UploadManyAction">
<!-- 配置文件上传的拦截器 -->
<interceptor-ref name="fileUpload">
<!-- 配置允许上传的文件类型为图片类型,txt,xls,docx -->
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,text/plain,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.wordprocessingml.document
</param>
<!-- 配置允许上传的文件大小 为2M,struts2文件上传默认的大小是2M,
如果需要上传超过2M的文件,在struts.xml配置文件中就需要加入这样一句<constant name="struts.multipart.maxSize" value="9000000"/> -->
<param name="maximumSize">2097152</param>
</interceptor-ref>
<!-- 配置系统默认的拦截器 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<!-- 这里的savePath对应了action里的savePath -->
<param name="savePath">/upload</param>
<!--定义逻辑视图和物理资源之间的映射 -->
<result name="success">/suc2.jsp</result>
<result name="error">/error.jsp</result>
<result name="input">/uploadinput.jsp</result>
</action>
</package>
</struts>
响应结果界面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<base href="<%=basePath%>">
<!-- 导入struts2标签库 -->
<%@taglib prefix="s" uri="/struts-tags"%>
<!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>上传成功</title>
</head>
<body>
<label>上传成功</label>
<!-- 输出上传的表单里的文件标题属性 -->
文件标题:<s:property value=" + title"/><br /><br><br>
<!-- 根据上传文件的文件名,在页面上显示上传的图片 -->
文件为:
<!-- 这里以上传三个图片举例 -->
<s:iterator value="#request.uploadFileName" status="indexValue" var="fileName">
<tr>
<td>
<!-- 输出索引值 -->
上传的第<s:property value="#indexValue.index+1"/>张图片<br><br>
<!-- 依次输出图片 -->
<img src="<s:property value="'upload/' + #fileName"/>"/><br/><br>
</td>
</tr>
</s:iterator>
</body>
</html>
另附:struts2文件上传(单文件上传)部分: http://blog.csdn.net/u011768325/article/details/45362687