国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

struts2中實現多個文件同時上傳代碼

2019-11-26 16:11:31
字體:
來源:轉載
供稿:網友

在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()+"http://"+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來封裝文件域參數。實現多個文件的同時上傳。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台东市| 茂名市| 茶陵县| 曲靖市| 屏山县| 西乌珠穆沁旗| 洪雅县| 桦南县| 浑源县| 科技| 安康市| 炉霍县| 九龙县| 城步| 于田县| 探索| 行唐县| 贵阳市| 潮安县| 和田市| 临武县| 延吉市| 南召县| 旺苍县| 六枝特区| 河源市| 炉霍县| 绍兴县| 丰镇市| 固阳县| 达尔| 瓮安县| 双桥区| 进贤县| 永春县| 永寿县| 高台县| 株洲县| 呼玛县| 大余县| 彭阳县|