一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - struts2實現多文件上傳

struts2實現多文件上傳

2021-05-28 13:34雪飄雪融 Java教程

這篇文章主要為大家詳細介紹了struts2實現多文件上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了struts2實現多文件上傳的具體代碼,供大家參考,具體內容如下

首先搭建好struts2的開發環境,導入struts2需要的最少jar包

struts2實現多文件上傳

新建upload.jsp頁面,注意一定要把表單的enctype設置成multipart/form-data

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<%@ 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>

新建一個uploadaction類,這個類主要有三個屬性,并為這三個屬性生成對應的set get方法

  • [file name] : 保存要上傳的文件
  • [file name]contenttype : 保存要上傳的文件類型
  • [file name]filename :保存上傳的文件名
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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;
/*
 * 多文件上傳要把對應的屬性類型都改為list集合,struts自動會把多個文件的數據封裝到里面
 */
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 {
 //遍歷文件集合,通過io流把每一個上傳的文件保存到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配置文件中配置一下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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><!-- 限制上傳的單個文件的大小 -->
  <param name="fileupload.allowedtypes">text/html,text/xml</param><!-- 限制上傳的文件的類型 -->
  <param name="fileupload.allowedextensions">txt,html,xml</param><!-- 限制上傳的文件的擴展名 -->
 </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目錄下新建一個message.properties文件定制錯誤消息

  • struts.messages.error.uploading - 文件不能被上傳
  • struts.messages.error.file.too.large - 文件超出大小
  • struts.messages.error.content.type.not.allowed - 文件類型不合法
  • struts.messages.error.file.extension.not.allowed - 文件擴展名不合法

struts2實現多文件上傳

顯示效果如下圖:

struts2實現多文件上傳

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/qq_22498277/article/details/51345283

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 69pao强力打造免费高速 | 国产精品酒店视频免费看 | 热辣小秘书办公室 | 成人网18免费网站 | 精品久久99麻豆蜜桃666 | 国产99久久久国产精品成人 | 色婷婷久久综合中文久久一本` | 爽爽窝窝午夜精品一区二区 | 日本mature乱子视频 | 欧美一区精品 | 欧美一级特黄特色大片免费 | 国产成人8x视频一区二区 | 包射屋 | 国产成人在线小视频 | 精品suv一区二区三区 | 美国美女hd18 | 性妲己| boobsmilking流奶水 | 成年私人影院免费视频网站 | 思思91精品国产综合在线 | 亚洲天堂视频在线观看免费 | 国产精品51麻豆cm传媒 | 亚洲欧美激情日韩在线 | 龟甲情感超市全文阅读 小说 | 日韩欧美一区二区三区四区 | 亚洲视频在线一区二区 | 好逼365| 日韩成人一级 | 日韩精品成人免费观看 | chinese腹肌gay| 女同全黄h全肉动漫 | 小鸟酱喷水 | www.色呦呦.com | 精品久久久久久影院免费 | 男插女的下面免费视频夜色 | 午夜一级视频 | 国产精品天天看天天爽 | 青青草国产精品免费 | 国产国语videosex另类 | 亚洲国产精品成 | 国产1广场舞丰满老女偷 |