struts2中實(shí)現(xiàn)多個(gè)文件同時(shí)上傳代碼
在upload.jsp頁面中將多個(gè)文件域?qū)ο竺麨橄嗤拿?,這樣在action中就可以將多個(gè)文件域解析成一個(gè)數(shù)組,數(shù)組的大小就是文件域的個(gè)數(shù),同時(shí)一個(gè)文件域解析成三個(gè)對(duì)應(yīng)的變量,因此多個(gè)文件域?qū)?yīng)三個(gè)數(shù)組,其中每個(gè)數(shù)組的大小就是文件域的個(gè)數(shù)。jsp頁面代碼如下:
<form action="upload.action" name="uploadForm" method="post" enctype="multipart/form-data">
文件標(biāo)題:<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>
對(duì)應(yīng)的Action依次遍歷所有文件域,然后生成對(duì)應(yīng)的輸入文件流,輸出文件流在指定的服務(wù)器保存路徑中添加對(duì)應(yīng)的輸出文件流保存文件。同時(shí)動(dòng)態(tài)指定服務(wù)器上文件的 保存路徑。
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文件配置如下:配置文件上傳的攔截器,允許 的上傳文件類型,上傳文件大小限制,同時(shí)引入defaultStack攔截器和上傳文件在服務(wù)器上的保存位置
<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過濾器,對(duì)文件的上傳功能沒有直接的影響,但是作為struts核心過濾器的輔助類是系統(tǒng)更加穩(wěn)定,消除未知的異常。
<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頁面代碼如下:
文件標(biāo)題:<s:property value="title"/><br/>
第一個(gè)圖片:<img alt="first" src="<s:property value="'upload/'+uploadFileName[0]"/>"/><br/>
第二個(gè)圖片:<img alt="second" src="<s:property value="'upload/'+uploadFileName[1]"/>"/><br/>
strus2同樣支持使用list同時(shí)上傳多個(gè)文件,其原理和數(shù)組是相同的,沒有根本的差別。只不過就是將數(shù)組全部改成list。并且修改list的訪問方法就可以用list來封裝文件域參數(shù)。實(shí)現(xiàn)多個(gè)文件的同時(shí)上傳。
- struts2+jsp+jquery+Jcrop實(shí)現(xiàn)圖片裁剪并上傳實(shí)例
- Struts2中圖片以base64方式上傳至數(shù)據(jù)庫(kù)
- Struts2+jquery.form.js實(shí)現(xiàn)圖片與文件上傳的方法
- Java以struts2為例介紹如何實(shí)現(xiàn)圖片上傳
- Struts2+uploadify多文件上傳實(shí)例
- JS+Struts2多文件上傳實(shí)例詳解
- struts2實(shí)現(xiàn)多文件上傳
- java中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- struts2單個(gè)文件上傳的兩種實(shí)現(xiàn)方式
- Java框架Struts2實(shí)現(xiàn)圖片上傳功能
相關(guān)文章
SpringDataMongoDB多文檔事務(wù)的實(shí)現(xiàn)
mongodb4.0也出來一段時(shí)間了,這個(gè)版本最為大眾期待的特性就是支持了多文檔事務(wù)。這篇文章主要介紹了SpringDataMongoDB多文檔事務(wù)的實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2018-11-11springboot利用aop實(shí)現(xiàn)接口異步(進(jìn)度條)的全過程
我們?cè)陂_發(fā)中,調(diào)用第三方接口時(shí),往往是提交數(shù)據(jù),要異步去獲取數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于springboot利用aop實(shí)現(xiàn)接口異步(進(jìn)度條)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01HttpServletRequestWrapper干預(yù)Request處理流程解析
這篇文章主要分析在?Tomcat的處理?http?請(qǐng)求的流程中干預(yù)?Request對(duì)象,?通過基于HttpServletRequestWrapper和?Filter組合進(jìn)行干預(yù),有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-09-09Mybatis查找返回Map,List集合類型的數(shù)據(jù)方式
這篇文章主要介紹了Mybatis查找返回Map,List集合類型的數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07使用Java實(shí)現(xiàn)MySQL數(shù)據(jù)鎖定的策略
在并發(fā)環(huán)境下,多個(gè)線程同時(shí)對(duì)MySQL數(shù)據(jù)庫(kù)進(jìn)行讀寫操作可能會(huì)導(dǎo)致數(shù)據(jù)沖突和不一致的問題,為了解決這些并發(fā)沖突,我們可以采用數(shù)據(jù)鎖定策略來保證數(shù)據(jù)的一致性和完整性,下面將介紹如何使用Java實(shí)現(xiàn)MySQL數(shù)據(jù)鎖定策略,,需要的朋友可以參考下2023-08-08解決IDEA中多模塊下Mybatis逆向工程不生成相應(yīng)文件的情況
這篇文章主要介紹了解決IDEA中多模塊下Mybatis逆向工程不生成相應(yīng)文件的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01java 避免出現(xiàn)NullPointerException(空指針)的方法總結(jié)
這篇文章主要介紹了java 避免出現(xiàn)NullPointerException(空指針)的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-09-09