==========================上传页面 index.jsp======================================
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:form theme="simple" method="post" action="manyFileUpload" enctype="multipart/form-data">
<table width="40%" border="1">
<tr>
<td>
your username:
</td>
<td>
<s:textfield name="userName"/>
</td>
</tr>
<tr>
<td>
your password:
</td>
<td>
<s:textfield name="password"/>
</td>
</tr>
<tr>
<td>
one file:
</td>
<td>
<s:file name="upload"/>
</td>
</tr>
<tr>
<td>
two file:
</td>
<td>
<s:file name="upload"/>
</td>
</tr>
<tr>
<td>
three file:
</td>
<td>
<s:file name="upload"/>
</td>
</tr>
<tr>
<td>
<s:submit value="上传"></s:submit>
</td>
<td>
<s:reset value="取消"></s:reset>
</td>
</tr>
</table>
</s:form>
</body>
</html>
===============================成功页面 success.jsp=====================================
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
username:<s:property value="userName"/><br/>
password:<s:property value="password"/><br/>
file Name:<s:property value="uploadFileName"/>
</body>
</html>
===========================================Action==========================
package org.demo.upload.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ManyFileUpload extends ActionSupport {
private String userName;
private String password;
private List<File> upload;
private List<String> uploadFileName;
private List<String> uploadContentType;
private String savePath;
//读取struts2中的配置《路径》
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(this.savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<File> getUpload() {
return upload;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public List<String> getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(List<String> uploadFileName) {
this.uploadFileName = uploadFileName;
}
public List<String> getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(List<String> uploadContentType) {
this.uploadContentType = uploadContentType;
}
@Override
public String execute() throws Exception {
for (int i = 0; i < upload.size(); i++) {
//获得要上传的路径,这~个是写死的
//String root=ServletActionContext.getRequest().getRealPath("/uploadFile");
File rootFile=new File(this.getSavePath());
if(!rootFile.exists())
rootFile.mkdirs();
//输入流,读取源文件
InputStream is=new FileInputStream(upload.get(i));
//输出流,上传文件
OutputStream os=new FileOutputStream(this.getSavePath()+"\\"+uploadFileName.get(i));
byte[] bytes=new byte[1024];
int len=0;
while((len=is.read(bytes))!=-1){
os.write(bytes, 0, len);
}
is.close();
os.close();
}
return SUCCESS;
}
}
===================================配置文件============================================
<?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">
<struts>
<!--设置编码 -->
<constant name="struts-i18n-encoding" value="GBK"/>
<!-- 设置资源 -->
<constant name="struts-custom-i18n-resources" value="globalMessages"/>
<package name="manyFileUpload" namespace="/" extends="struts-default">
<action name="manyFileUpload" class="org.demo.upload.servlet.ManyFileUpload">
<!-- 配置文件上传过滤 -->
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/pjpeg,image/gif,image/x-png,image/jpeg,image/png</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<param name="savePath">/uploadFile</param>
<result name="success">/two_upload/success.jsp</result>
<result name="input">/two_upload/index.jsp</result>
</action>
</package>
</struts>