struts2中實現多個檔案同時上傳代碼_java

來源:互聯網
上載者:User

在upload.jsp頁面中將多個檔案域對象命名為相同的名字,這樣在action中就可以將多個檔案域解析成一個數組,數組的大小就是檔案域的個數,同時一個檔案域解析成三個對應的變數,因此多個檔案域對應三個數組,其中每個數組的大小就是檔案域的個數。jsp頁面代碼如下:

複製代碼 代碼如下:

<form action="upload.action" name="uploadForm" method="post" enctype="multipart/form-data">
檔案標題:<input type="text" name="title"/><br/>
選擇檔案-:<input type="file" name="upload"/><br/>
選擇檔案二:<input type="file" name="upload"/><br/>
選擇檔案三:<input type="file" name="upload"/><br/>
<input type="submit" value="upload"/>

</form>

對應的Action依次遍曆所有檔案域,然後產生對應的輸入檔案流,輸出檔案流在指定的伺服器儲存路徑中添加對應的輸出檔案流儲存檔案。同時動態指定伺服器上檔案的 儲存路徑。

action代碼如下:

複製代碼 代碼如下:

package com.inspur.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

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 ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String upload()throws Exception{
File[] files=this.getUpload();
for(int i=0;i<files.length;i++){
FileOutputStream fos=new FileOutputStream(this.getSavePath()+"\\"+this.getUploadFileName()[i]);
byte[] buffer=new byte[1024];
FileInputStream fis=new FileInputStream(files[i]);
int len=0;
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
}


return SUCCESS;
}

}

struts.xml檔案配置如下:設定檔上傳的攔截器,允許 的上傳檔案類型,上傳檔案大小限制,同時引入defaultStack攔截器和上傳檔案在伺服器上的儲存位置

複製代碼 代碼如下:

<struts>
<constant name="struts.custom.i18n.resources" value="message"></constant>
<constant name="struts.i18n.encoding" value="gbk"></constant>
<package name="uploadMult" extends="struts-default" namespace="/">
<action name="upload" class="com.inspur.action.UploadAction" method="upload">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
<param name="maximumSize">20000000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<param name="savePath">/upload</param>
<result name="success">/success.jsp</result>
<result name="input">/upload.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>

</struts>


web.xml檔案代碼如下:配置了struts-cleanup過濾器,對檔案的上傳功能沒有直接的影響,但是作為struts核心過濾器的輔助類是系統更加穩定,消除未知的異常。
複製代碼 代碼如下:

<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

在上傳成功介面顯示所有上傳的圖片:

success.jsp頁面代碼如下:

複製代碼 代碼如下:

檔案標題:<s:property value="title"/><br/>
第一個圖片:<img alt="first" src="<s:property value="'upload/'+uploadFileName[0]"/>"/><br/>
第二個圖片:<img alt="second" src="<s:property value="'upload/'+uploadFileName[1]"/>"/><br/>

strus2同樣支援使用list同時上傳多個檔案,其原理和數組是相同的,沒有根本的差別。只不過就是將數組全部改成list。並且修改list的存取方法就可以用list來封裝檔案域參數。實現多個檔案的同時上傳。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.