1.创建Action类ListUpload.java
package action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Random;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ListUpload extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 4709924287629100425L;
// 使用List封装多个上传文件域的对应的文件内容
private List<File> doc;
// 使用List封装多个上传文件域对应的文件类型
private List<String> docContentType;
// 使用List封装多个上传文件域对应的文件名
private List<String> docFileName;
private String path;
public List<File> getDoc() {
return doc;
}
public void setDoc(List<File> doc) {
this.doc = doc;
}
public List<String> getDocContentType() {
return docContentType;
}
public void setDocContentType(List<String> docContentType) {
this.docContentType = docContentType;
}
public List<String> getDocFileName() {
return docFileName;
}
public void setDocFileName(List<String> docFileName) {
this.docFileName = docFileName;
}
public void setPath(String value) {
this.path = value;
}
public String getPath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(path);
}
private String getFileName(String fileName){
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
int random = new Random().nextInt(1000);
return "" + System.currentTimeMillis() + random + extension;
}
@SuppressWarnings("resource")
@Override
public String execute() throws Exception {
for (int i = 0; i < doc.size(); i++) { //循环遍历整个
docFileName.set(i, getFileName(docFileName.get(i)));
FileOutputStream fos = new FileOutputStream(getPath() + "\\" + docFileName.get(i));
File file = doc.get(i);
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[1024];
int length = 0;
while ((length = fis.read(b)) > 0) {
fos.write(b, 0, length);
}
}
return super.execute();
}
}
2.struts.xml中注册action
<action name="listUpload" class="action.ListUpload">
<!-- 上传文件拦截,只能上传图片资源 -->
<interceptor-ref name="fileUpload">
<!--allowedTypes允许上传的文件类型 -->
<param name="allowedTypes">
image/x-png,image/gif,image/bmp,image/pjpeg
</param>
<!-- 上传文件的大小单位 字节 -->
<param name="maximumSize">30000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<param name="path">/upload</param>
<result name="success">/listSuccess.jsp</result>
<result name="input">/listUpload.jsp</result>
</action>
在src/action包下创建文件,命名格式为ActionName-conversion.properties。如ListUpload-conversion.properties,然后在该文件下添加如下内容:
Element_doc=java.io.File
其中doc为Action中封装的doc属性。
之所以这样做,是因为程序可能在运行的时候会报类型不匹配异常。
3.上传页面
listUpload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Struts 2使用List实现多个文件上传</title>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<center>
<div>
<%@ include file="top.html" %>
</div>
<h3>Struts 2使用List实现多个文件上传</h3>
<hr/>
<s:form action="listUpload" enctype="multipart/form-data">
<s:file name="doc" label="选择上传文件"/>
<s:file name="doc" label="选择上传文件"/>
<s:file name="doc" label="选择上传文件"/>
<s:submit value="上传"/>
</s:form>
</center>
</body>
</html>
成功页面
listSuccess.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>使用List上传多个文件成功</title>
<link rel="stylesheet" type="text/css" href="Style.css">
<STYLE type="text/css">
<!--
ul.myUl{width: 450px;}
ul.myUl li{width: 150px;float: left;}
//-->
</STYLE>
</head>
<body>
<center>
<div>
<%@ include file="top.html" %>
</div>
<h3>使用List上传多个文件成功</h3>
<hr/>
<ul class="myUl">
<s:iterator value="docFileName" status="st">
<li>
第<s:property value="#st.getIndex()+1"/>个图片:
<br/>
<img src="upload/<s:property value="docFileName.get(#st.getIndex())"/>"/>
</li>
</s:iterator>
</ul>
</center>
</body>
</html>
运行程序。