Struts2實(shí)現(xiàn)多文件上傳功能
前臺(tái)form 表單:設(shè)置method=post,enctype=multipart/form-data。
struts2在原有的上傳解析器繼承上做了進(jìn)一步封裝,更進(jìn)一步簡(jiǎn)化了文件上傳。
Action需要使用3個(gè)屬性來封裝該文件域的信息:
(1)類型為File的*屬性封裝了該文件域?qū)?yīng)的文件內(nèi)容;
(2)類型為String的***FileName屬性封裝了該文件域?qū)?yīng)的文件的文件類型;
(3)類型為String的***ContentType屬性封裝了該文件域?qū)?yīng)的文件的類型。
具體實(shí)現(xiàn):
新建web項(xiàng)目

添加struts2相關(guān)包
myeclipse可直接下載,右擊項(xiàng)目,如下。

前臺(tái)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
<input type="file" name="upload" multiple="multiple"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>UploadFile</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="true"/>
<constant name="struts.multipart.saveDir" value="/tmp"/>
<constant name="struts.custom.i18n.resources" value="app"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="com.yf.action.UploadAction">
<result>/index.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
后臺(tái)代碼
public class UploadAction extends ActionSupport{
private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;
public List<File> getUpload() {
return upload;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public List<String> getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(List<String> uploadContentType) {
this.uploadContentType = uploadContentType;
}
public List<String> getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(List<String> uploadFileName) {
this.uploadFileName = uploadFileName;
}
@Override
public String execute() throws Exception {
//文件保存路徑
String path = ServletActionContext.getServletContext().getRealPath("/images");
File file = new File(path);
//不存在則創(chuàng)建
if(!file.exists()){
file.mkdir();
}
//循環(huán)將文件上傳到指定路徑
for(int i = 0; i< upload.size(); i++){
FileUtils.copyFile(upload.get(i), new File(file,uploadFileName.get(i)));
}
return SUCCESS;
}
結(jié)果如下


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- struts2單個(gè)文件上傳的兩種實(shí)現(xiàn)方式
- Java以struts2為例介紹如何實(shí)現(xiàn)圖片上傳
- struts2中實(shí)現(xiàn)多個(gè)文件同時(shí)上傳代碼
- 關(guān)于Struts2文件上傳與自定義攔截器
- Struts2+jquery.form.js實(shí)現(xiàn)圖片與文件上傳的方法
- JavaWeb中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- JavaEE中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- java中struts2實(shí)現(xiàn)簡(jiǎn)單的文件上傳與下載
- struts2+jsp實(shí)現(xiàn)文件上傳的方法
相關(guān)文章
Spring Cloud之服務(wù)監(jiān)控turbine的示例
這篇文章主要介紹了Spring Cloud之服務(wù)監(jiān)控turbine的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
java高并發(fā)InterruptedException異常引發(fā)思考
這篇文章主要為大家介紹了java高并發(fā)InterruptedException異常引發(fā)思考,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
淺談Spring Boot 2.0遷移指南主要注意點(diǎn)
Spring官方的Spring Boot 2變動(dòng)指南,主要是幫助您將應(yīng)用程序遷移到Spring Boot 2.0,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
簡(jiǎn)單談?wù)刯ava的異常處理(Try Catch Finally)
在程序設(shè)計(jì)中,進(jìn)行異常處理是非常關(guān)鍵和重要的一部分。一個(gè)程序的異常處理框架的好壞直接影響到整個(gè)項(xiàng)目的代碼質(zhì)量以及后期維護(hù)成本和難度。2016-03-03
SpringBoot在一定時(shí)間內(nèi)限制接口請(qǐng)求次數(shù)的實(shí)現(xiàn)示例
在項(xiàng)目中,接口的暴露在外面,很多人就會(huì)惡意多次快速請(qǐng)求,本文主要介紹了SpringBoot在一定時(shí)間內(nèi)限制接口請(qǐng)求次數(shù)的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2022-03-03
Java 異步回調(diào)機(jī)制實(shí)例分析
這篇文章主要介紹了Java 異步回調(diào)機(jī)制實(shí)例解析的相關(guān)資料,需要的朋友可以參考下2017-02-02
三種java編程方法實(shí)現(xiàn)斐波那契數(shù)列
這篇文章主要為大家詳細(xì)介紹了三種java編程方法實(shí)現(xiàn)斐波那契數(shù)列,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
單元測(cè)試 @mock與@SpringBootTest的使用
這篇文章主要介紹了單元測(cè)試 @mock與@SpringBootTest的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

