首页 > 代码库 > struts2-18-上传多文件
struts2-18-上传多文件
上传多文件:数组或者集合
一:上传文件的主页面 upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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>
<s:fielderror/>
<form action="uploadAction" method="post" enctype="multipart/form-data">
<table border="1">
<caption>上传文件</caption>
<tr>
<th>上传标题</th>
<td><input type="text" name="title"></td>
</tr>
<tr>
<th>选择文件1:</th>
<td><input type="file" name="upload"></td>
</tr>
<tr>
<th>选择文件2:</th>
<td><input type="file" name="upload"></td>
</tr>
<tr>
<th>选择文件3:</th>
<td><input type="file" name="upload"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="http://www.mamicode.com/上传"></td>
</tr>
</table>
</form>
</body>
</html>
二:跳转到相应的action nuc.sw.action---uploadAction.java
package nuc.sw.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.UUID;
import com.opensymphony.xwork2.ActionSupport;
public class uploadAction extends ActionSupport {
private String title;
private File[] upload;
private String[] uploadFileName;
private String[] uploadContentType;
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
@Override
public String execute() throws Exception {
System.out.println(title);
System.out.println(upload);
System.out.println(uploadFileName);
System.out.println(uploadContentType);
System.out.println(savePath);
for(int i=0;i<upload.length;i++){
//输入流
FileInputStream fis=new FileInputStream(upload[i]);
//输出流
//上传两次一样的都会成功且显示两个,所以解决覆盖问题
FileOutputStream fos=new FileOutputStream(savePath+"/"+UUID.randomUUID().toString()+"_"+uploadFileName[i]);
//字节为单位上传
byte[] buffer=new byte[1024];
//定义长度
int len=0;
//从0开始
while((len=fis.read(buffer))>0){
fos.write(buffer, 0, len);
}
}
//不能写输入输出流
return SUCCESS;
}
}
三:配置struts.xml
1: 对文件的类型 大小进行限制(实现文件过滤)
2:设置上传的文件的真实路径 即将.tmp文件保存到savepath文件中
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <constant name="struts.devMode" value="http://www.mamicode.com/true" /> 8 <!-- 设置文件的临时保存路径 --> 9 <constant name="struts.multipart.saveDir" value="d:/tmpsaveDir"></constant> 10 <package name="default" namespace="/" extends="struts-default"> 11 <action name="uploadAction" class="nuc.sw.action.uploadAction"> 12 <!-- 需要去找源码:struts-default.xml中寻找拦截器 再关联源码 --> 13 <interceptor-ref name="fileUpload"> 14 <!--设置上传的文件类型 --> 15 <!-- 隐藏这一属性后,黑客可以随意上传修改后缀名的病毒文件 (实质没有改变)--> 16 <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/phpeg,text/plain</param> 17 <!-- 设置上传的文件的大小 --> 18 <param name="maximumSize">65535</param> 19 <!-- 设置上传文件的扩展名 --> 20 <param name="allowedExtensions">.txt</param> 21 </interceptor-ref> 22 <!-- 引用默认拦截器时,需要使用defaultStack --> 23 <interceptor-ref name="defaultStack"/> 24 <!-- 设置保存的真实路径 --> 25 <param name="savePath">d:/uploadRealFile</param> 26 <result name="success">/hello.jsp</result> 27 <result name="input">/upload.jsp</result> 28 </action> 29 </package> 30 </struts>
四:项目结构
五:运行结果
struts2-18-上传多文件