参照上一篇博文《strut2 文件上传》,多文件上传只需要将属性类型改为数组类型或者list类型即可。
用来上传的web页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>Insert title here</title>
</head>
<body>
<s:form action="multipleUpload.action" method="post" enctype="multipart/form-data">
<s:file label="图片一" name="myFile" ></s:file>
<s:file label="图片二" name="myFile" ></s:file>
<s:file label="图片三" name="myFile" ></s:file>
<s:file label="图片四" name="myFile" ></s:file>
<s:file label="图片五" name="myFile" ></s:file>
<s:submit value="提交" />
</s:form>
</body>
</html>
处理的Action:
package com.student.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Random;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class MultipleUpLoadAction extends ActionSupport {
private static final int BUFFER_SIZE = 2 * 1024;
private File[] myFile;
private String[] myFileFileName;
private String[] myFileContentType;
public File[] getMyFile() {
return myFile;
}
public void setMyFile(File[] myFile) {
this.myFile = myFile;
}
public String[] getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String[] myFileFileName) {
this.myFileFileName = myFileFileName;
}
public String[] getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String[] myFileContentType) {
this.myFileContentType = myFileContentType;
}
public String execute() throws Exception {
//原文件
File[] src = getMyFile();
//目标地址
String dstPath = ServletActionContext.getServletContext().getRealPath("/")+"/images";
//循环存放原文件
for(int i=0;i<src.length;i++){
//给源文件重新名称
String imageFileName = getImageFileName(getMyFileFileName()[i]);
ServletActionContext.getRequest().setAttribute("imageFileName"+(i+1), imageFileName);
//声明目标文件
File dst = new File(dstPath+"/"+imageFileName);
//将源文件拷贝到目标地
copy(src[i], dst);
}
return SUCCESS;
}
//将指定的文件拷贝到指定的目录
public static void copy(File src,File dst){
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE);
byte[] b = new byte[1024];
int len = 0;
while((len = in.read(b))!= -1){
out.write(b, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(null!=in){
in.close();
}
if(null!=out) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//根据当前时间给文件取指定的名字
public static String getImageFileName(String fileName){
Random rnd = new Random();
int rn = rnd.nextInt(1000);
return new Date().getTime()+ rn + getExtention(fileName);
}
//获取文件的后缀
public static String getExtention(String fileName){
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
}
成功后返回的页面:
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div style="padding: 3px; border: solid 1px #cccccc; text-align: center">
<img src="images/${imageFileName1}" />
<br/>
<img src="images/${imageFileName2}" />
<br/>
<img src="images/${imageFileName3}" />
<br/>
<img src="images/${imageFileName4}" />
<br/>
<img src="images/${imageFileName5}" />
</div>
</body>
</html>
配置Action的文件:
<action name="upload" class="com.student.action.UpLoadAction1">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/pjpeg,image/jpg,image/JPG
</param>
<param name="maximumSize">1048576</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="success">/success.jsp</result>
<result name="input">/upload.jsp</result>
</action>