標籤:關聯 尾碼名 項目結構 support msi odi style nbsp const
上傳多檔案:數組或者集合
一:上傳檔案的首頁面 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="上傳"></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="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-上傳多檔案