本文实例为大家分享了 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