本文實例為大家分享了struts2實現多文件上傳的具體代碼,供大家參考,具體內容如下
首先搭建好struts2的開發環境,導入struts2需要的最少jar包
新建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 - 文件擴展名不合法
顯示效果如下圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_22498277/article/details/51345283