struts2實(shí)現(xiàn)多文件上傳
本文實(shí)例為大家分享了struts2實(shí)現(xiàn)多文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
首先搭建好struts2的開(kāi)發(fā)環(huán)境,導(dǎo)入struts2需要的最少jar包
新建upload.jsp頁(yè)面,注意一定要把表單的enctype設(shè)置成multipart/form-data
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'upload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <s:fielderror name="fieldErrors"></s:fielderror> <s:form theme="simple" action="upload" enctype="multipart/form-data" method="post"> file:<s:file name="file"></s:file> fileDesc:<s:textfield name="fileDesc[0]"></s:textfield> <br/><br/> file:<s:file name="file"></s:file> fileDesc:<s:textfield name="fileDesc[1]"></s:textfield> <br/><br/> file:<s:file name="file"></s:file> fileDesc:<s:textfield name="fileDesc[2]"></s:textfield> <s:submit></s:submit> </s:form> </body> </html>
新建一個(gè)UploadAction類(lèi),這個(gè)類(lèi)主要有三個(gè)屬性,并為這三個(gè)屬性生成對(duì)應(yīng)的set get方法
- [File Name] : 保存要上傳的文件
- [File Name]ContentType : 保存要上傳的文件類(lèi)型
- [File Name]FileName :保存上傳的文件名
package cn.lfd.web.upload; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /* * 多文件上傳要把對(duì)應(yīng)的屬性類(lèi)型都改為L(zhǎng)ist集合,struts自動(dòng)會(huì)把多個(gè)文件的數(shù)據(jù)封裝到里面 */ public class UploadAction extends ActionSupport { private static final long serialVersionUID = 1L; private List<File> file; private List<String> fileContentType; private List<String> fileFileName; private List<String> fileDesc; public List<File> getFile() { return file; } public void setFile(List<File> file) { this.file = file; } public List<String> getFileContentType() { return fileContentType; } public void setFileContentType(List<String> fileContentType) { this.fileContentType = fileContentType; } public List<String> getFileFileName() { return fileFileName; } public void setFileFileName(List<String> fileFileName) { this.fileFileName = fileFileName; } public List<String> getFileDesc() { return fileDesc; } public void setFileDesc(List<String> fileDesc) { this.fileDesc = fileDesc; } @Override public String execute() throws Exception { //遍歷文件集合,通過(guò)IO流把每一個(gè)上傳的文件保存到upload文件夾下面 for(int i=0;i<file.size();i++) { //得到要保存的文件路徑 String dir = ServletActionContext.getServletContext().getRealPath("/upload/"+fileFileName.get(i)); OutputStream out = new FileOutputStream(dir); InputStream in = new FileInputStream(file.get(i)); byte[] flush = new byte[1024]; int len = 0; while((len=in.read(flush))!=-1) { out.write(flush, 0, len); } in.close(); out.close(); } return "input"; } }
然后在struts.xml配置文件中配置一下
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="message"></constant> <package name="default" extends="struts-default"> <interceptors> <interceptor-stack name="lfdstack"> <interceptor-ref name="defaultStack"> <param name="fileUpload.maximumSize">200000</param><!-- 限制上傳的單個(gè)文件的大小 --> <param name="fileUpload.allowedTypes">text/html,text/xml</param><!-- 限制上傳的文件的類(lèi)型 --> <param name="fileUpload.allowedExtensions">txt,html,xml</param><!-- 限制上傳的文件的擴(kuò)展名 --> </interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="lfdstack"></default-interceptor-ref> <action name="upload" class="cn.lfd.web.upload.UploadAction"> <result>/success.jsp</result> <result name="input">/upload.jsp</result> </action> </package> </struts>
在src目錄下新建一個(gè)message.properties文件定制錯(cuò)誤消息
- struts.messages.error.uploading - 文件不能被上傳
- struts.messages.error.file.too.large - 文件超出大小
- struts.messages.error.content.type.not.allowed - 文件類(lèi)型不合法
- struts.messages.error.file.extension.not.allowed - 文件擴(kuò)展名不合法
顯示效果如下圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 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í)例詳解
- java中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- struts2單個(gè)文件上傳的兩種實(shí)現(xiàn)方式
- struts2中實(shí)現(xiàn)多個(gè)文件同時(shí)上傳代碼
- Java框架Struts2實(shí)現(xiàn)圖片上傳功能
相關(guān)文章
打開(kāi).properties中文顯示unicode編碼問(wèn)題以及解決
這篇文章主要介紹了打開(kāi).properties中文顯示unicode編碼問(wèn)題以及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Spring?Boot使用MyBatis進(jìn)行兩個(gè)表的關(guān)聯(lián)
本文主要介紹了Spring?Boot使用MyBatis進(jìn)行兩個(gè)表的關(guān)聯(lián),通過(guò)實(shí)例演示了如何使用MyBatis的XML映射文件和注解實(shí)現(xiàn)關(guān)聯(lián)操作,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09MyBatis 添加元數(shù)據(jù)自定義元素標(biāo)簽的實(shí)現(xiàn)代碼
這篇文章主要介紹了MyBatis 添加元數(shù)據(jù)自定義元素標(biāo)簽的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07(starters)springboot-starter整合阿里云datahub方式
這篇文章主要介紹了(starters)springboot-starter整合阿里云datahub方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Spring學(xué)習(xí)通過(guò)AspectJ注解方式實(shí)現(xiàn)AOP操作
這篇文章主要為大家介紹了Spring學(xué)習(xí)通過(guò)AspectJ注解方式實(shí)現(xiàn)AOP操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Mybatis使用collection標(biāo)簽進(jìn)行樹(shù)形結(jié)構(gòu)數(shù)據(jù)查詢(xún)時(shí)攜帶外部參數(shù)查詢(xún)
這篇文章主要介紹了Mybatis使用collection標(biāo)簽進(jìn)行樹(shù)形結(jié)構(gòu)數(shù)據(jù)查詢(xún)時(shí)攜帶外部參數(shù)查詢(xún),需要的朋友可以參考下2023-10-10Java中final關(guān)鍵字和final的4種用法
這篇文章主要給大家分享的是?Java中final關(guān)鍵字和final的4種用法修飾類(lèi)、修飾方法、修飾變量、修飾參數(shù),下面文章具有一定的參考價(jià)值,需要的小伙伴可以參考一下2021-11-11