Struts2(五)
以下内容是基于导入struts2-2.3.32.jar包来讲的
1.文件上传
A.单文件上传
<body> <form action="${pageContext.request.contextPath }/one" method="post" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit" value="上传"/> </form> </body>
1 package com.rong.web.action;
2
3 import java.io.File;
4
5 import org.apache.commons.io.FileUtils;
6
7 import com.opensymphony.xwork2.ActionSupport;
8
9 public class MyAction extends ActionSupport {
10 private static final long serialVersionUID = -351587239525292420L;
11 //保存上传的文件对象,file对应表单元素名称,名称必须一致,拦截器会解析这个格式!
12 private File file;
13 //文件名,必须为fileFileName
//FileName 固定的写法。必须为file+FileName
14 private String fileFileName;
15 //ContentType 固定的写法。必须为file+ContentType
16 private String fileContentType;
17 public File getFile() {
18 return file;
19 }
20 public void setFile(File file) {
21 this.file = file;
22 }
23
24 public String getFileFileName() {
25 return fileFileName;
26 }
27 public void setFileFileName(String fileFileName) {
28 this.fileFileName = fileFileName;
29 }
30 public String getFileContentType() {
31 return fileContentType;
32 }
33 public void setFileContentType(String fileContentType) {
34 this.fileContentType = fileContentType;
35 }
36 @Override
37 public String execute() throws Exception {
38 //E:\apache-tomcat-7.0.82\work\Catalina\localhost\struts2\ upload_1742945b_24ed_4612_a2f4_b83cffa61620_00000000.tmp
39 //struts会保存到Tomcat服务器的struts2文件夹中创建临时文件,若不处理,执行完代码会把此临时文件删除
40 System.out.println(file.getAbsolutePath());
41 //butterfly.jpg
42 System.out.println(fileFileName);
43 //image/png
44 System.out.println(fileContentType);
45 File targetFile=new File("c:/", fileFileName);
46 FileUtils.copyFile(file, targetFile);
47 return SUCCESS;
48 }
49 }
文件类型: mime-type想了解的可以去Tomcat服务器的web.xml文件查看,里面有文件类型配置
E:\apache-tomcat-7.0.82\conf\web.xml
限制文件上传大小:
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (81498) exceeds the configured maximum (1024)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置限制文件上传大小为1K -->
<constant name="struts.multipart.maxSize" value="1024"></constant>
<package name="default" namespace="/" extends="struts-default" >
<global-results>
<!-- struts在文件上传失败的时候,会返回input的错误视图,在这里配置对应的页面! -->
<result name="input">/input.jsp</result>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<action name="one" class="com.rong.web.action.MyAction">
<result>/one.jsp</result>
</action>
</package>
</struts>
B.多文件上传
input的name属性值必须一致
<body>
<form action="${pageContext.request.contextPath }/one" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="file" name="file"/>
<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
</body>
1 package com.rong.web.action; 2 3 import java.io.File; 4 5 import org.apache.commons.io.FileUtils; 6 7 import com.opensymphony.xwork2.ActionSupport; 8 9 public class MyAction extends ActionSupport { 10 private static final long serialVersionUID = -351587239525292420L; 11 //保存上传的文件对象,file对应表单元素名称,名称必须一致,拦截器会解析这个格式! 12 private File[] file; 13 //文件名,必须为fileFileName 14 private String[] fileFileName; 15 //文件类型,必须为fileContentType 16 private String[] fileContentType; 17 18 public File[] getFile() { 19 return file; 20 } 21 22 public void setFile(File[] file) { 23 this.file = file; 24 } 25 26 public String[] getFileFileName() { 27 return fileFileName; 28 } 29 30 public void setFileFileName(String[] fileFileName) { 31 this.fileFileName = fileFileName; 32 } 33 34 public String[] getFileContentType() { 35 return fileContentType; 36 } 37 38 public void setFileContentType(String[] fileContentType) { 39 this.fileContentType = fileContentType; 40 } 41 42 @Override 43 public String execute() throws Exception { 44 if(file!=null){ 45 for(int i=0;i<file.length;i++){ 46 System.out.println(file[i]); 47 System.out.println(fileFileName[i]); 48 System.out.println(fileContentType[i]); 49 File targetFile=new File("c:/", fileFileName[i]); 50 FileUtils.copyFile(file[i], targetFile); 51 } 52 } 53 return SUCCESS; 54 } 55 }
E:\apache-tomcat-7.0.82\work\Catalina\localhost\struts2\upload_ca4933df_aa32_4fc4_bbd5_d5d887c68972_00000006.tmp 2018届毕业设计2017-6-8.rar application/octet-stream E:\apache-tomcat-7.0.82\work\Catalina\localhost\struts2\upload_ca4933df_aa32_4fc4_bbd5_d5d887c68972_00000007.tmp nio1.png image/png E:\apache-tomcat-7.0.82\work\Catalina\localhost\struts2\upload_ca4933df_aa32_4fc4_bbd5_d5d887c68972_00000008.tmp 容杰龙.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 配置限制文件上传大小为1000k --> <constant name="struts.multipart.maxSize" value="10240000"></constant> <package name="default" namespace="/" extends="struts-default" > <global-results> <!-- struts在文件上传失败的时候,会返回input的错误视图,在这里配置对应的页面! --> <result name="input">/input.jsp</result> <result name="error">/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings> <action name="one" class="com.rong.web.action.MyAction"> <result>/one.jsp</result> </action> </package> </struts>
限制允许上传的文件类型以及文件扩展名:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置限制文件上传大小为1000k -->
<constant name="struts.multipart.maxSize" value="10240000"></constant>
<package name="default" namespace="/" extends="struts-default" >
<global-results>
<!-- struts在文件上传失败的时候,会返回input的错误视图,在这里配置对应的页面! -->
<result name="input">/input.jsp</result>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<action name="one" class="com.rong.web.action.MyAction">
<!-- Action里设置默认拦截器栈 -->
<interceptor-ref name="defaultStack">
<!-- 限制允许上传的文件类型 -->
<param name="fileUpload.allowedTypes">image/png,text/plain</param>
<!-- 限制上传文件的扩展名 -->
<param name="fileUpload.allowedExtensions">txt,png,docx</param>
<!-- 以上两个参数同时配置取交集 -->
</interceptor-ref>
<result>/one.jsp</result>
</action>
</package>
</struts>
如果出现异常,上传失败,却无法弹出jsp页面,可在Tomcat服务器的server.xml文件中修改:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8" maxSwallowSize="-1"/>
2.文件下载
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default" >
<action name="download" class="com.rong.web.action.MyAction">
<!-- 下载操作,name可以随意,但需要与返回值对应。type类型唯一。 -->
<result name="down" type="stream">
<!-- 文件的类型,指定为任意二进制类型,可以标识任何文件 -->
<param name="contentType">application/octet-stream</param>
<!-- 对应的是Action类中返回流的属性!对应getFileStream()方法 -->
<param name="inputName">fileStream</param>
<!-- 指定浏览器显示的文件名,对应action类中的返回文件名的属性!需要url编码,对应getDownFile()方法 -->
<param name="contentDisposition">attachment;filename=${downFile}</param>
<!-- 读取缓冲区大小 -->
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
1 package com.rong.web.action;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.InputStream;
7 import java.io.UnsupportedEncodingException;
8 import java.net.URLEncoder;
9
10 import com.opensymphony.xwork2.ActionSupport;
11
12 public class MyAction extends ActionSupport {
13 private static final long serialVersionUID = -351587239525292420L;
14 private String filePath;
15 public String getFilePath() {
16 return filePath;
17 }
18 public void setFilePath(String filePath) {
19 this.filePath = filePath;
20 }
21 @Override
22 public String execute() throws Exception {
23 //实际开发中,用户只要传文件路径即可实现下载,这里固定路径。
24 filePath="C:/2017.11.17-广州市部份培训公司名单.docx";
25 return "down";
26 }
27 //返回流<param name="inputName">fileStream</param>
28 public InputStream getFileStream() throws FileNotFoundException{
29 return new FileInputStream(filePath);
30 }
31 //<param name="contentDisposition">attachment;filename=${downFile}</param>
32 public String getDownFile() throws UnsupportedEncodingException{
33 File file=new File(filePath);
34 String name = file.getName();
35 return URLEncoder.encode(name, "UTF-8");
36 }
37 }