(八)Struts2 文件上传和下载
所有的学习我们必须先搭建好Struts2的环境(1、导入对应的jar包,2、web.xml,3、struts.xml)
第一节:Struts2 文件上传
Struts2 文件上传基于Struts2 拦截器实现;
Struts2 文件上传使用的是fileupload 组件;
Form 配置enctype="multipart/form-data";(这样就是以二进制来上传的)
Struts2 获取上传文件:name (name 是文件表单的name)
Struts2 获取上传文件名:name+FileName;
Struts2 获取上传文件的类型:name+ContentType;
例子:
struts.xml
<?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> <package name="manager" extends="struts-default"> <action name="upload" class="com.wishwzp.action.FileUploadAction"> <result name="success">/success.jsp</result> </action> </package> </struts>
FileUploadAction.java
1 package com.wishwzp.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 FileUploadAction extends ActionSupport{ 10 11 /** 12 * 13 */ 14 private static final long serialVersionUID = 1L; 15 16 // 文件(这里的文件名必须要和前端的文件:<input type="file" name="wishwzp"/><br/>name一样) 17 private File wishwzp; 18 19 // 文件名(name+FileName) 20 private String wishwzpFileName; 21 22 // 文件类型(name+ContentType) 23 private String wishwzpContentType; 24 25 public File getwishwzp() { 26 return wishwzp; 27 } 28 29 public void setwishwzp(File wishwzp) { 30 this.wishwzp = wishwzp; 31 } 32 33 public String getwishwzpFileName() { 34 return wishwzpFileName; 35 } 36 37 public void setwishwzpFileName(String wishwzpFileName) { 38 this.wishwzpFileName = wishwzpFileName; 39 } 40 41 public String getwishwzpContentType() { 42 return wishwzpContentType; 43 } 44 45 public void setwishwzpContentType(String wishwzpContentType) { 46 this.wishwzpContentType = wishwzpContentType; 47 } 48 49 @Override 50 public String execute() throws Exception { 51 System.out.println("文件名:"+wishwzpFileName); 52 System.out.println("文件类型:"+wishwzpContentType); 53 //上传到C盘中 54 String filePath="C:/"+wishwzpFileName; 55 //保存文件添加到文件中 56 File saveFile=new File(filePath); 57 //将wishwzp文件拷到saveFile中,这样数据就到C盘中了 58 FileUtils.copyFile(wishwzp, saveFile); 59 return SUCCESS; 60 } 61 62 63 64 65 }
fileupload.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <!-- 这里enctype="multipart/form-data就是将数据以二进制上传 --> 12 <form action="upload" method="post" enctype="multipart/form-data"> 13 文件:<input type="file" name="wishwzp"/><br/> 14 <input type="submit" value="提交"/> 15 </form> 16 </body> 17 </html>
success.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 文件上传成功! 12 </body> 13 </html>
第二节:配置文件的大小及类型
配置可以上传哪些文件的信息以及最大上传的大小
<param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param>
<param name="maximumSize">81101</param>
例子:
struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 <package name="manager" extends="struts-default"> 9 10 <action name="upload" class="com.wishwzp.action.FileUploadAction"> 11 <result name="success">/success.jsp</result> 12 <result name="input">/fileupload.jsp</result> 13 14 <interceptor-ref name="fileUpload"> 15 <param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param> 16 <param name="maximumSize">81101</param> 17 </interceptor-ref> 18 19 <interceptor-ref name="defaultStack"></interceptor-ref> 20 21 </action> 22 23 </package> 24 25 </struts>
前端页面文件错误表达式:
<s:fielderror></s:fielderror>
例子:
fileupload.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <s:fielderror></s:fielderror> 12 <!-- 这里enctype="multipart/form-data就是将数据以二进制上传 --> 13 <form action="upload" method="post" enctype="multipart/form-data"> 14 文件:<input type="file" name="wishwzp"/><br/> 15 <input type="submit" value="提交"/> 16 </form> 17 </body> 18 </html>
第三节:大文件上传
Struts2 文件上传大小默认是2M;
<constant name="struts.multipart.maxSize" value="20000000"></constant>
例子:
struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 <!-- 上传大小配置 --> 9 <constant name="struts.multipart.maxSize" value="20000000"></constant> 10 11 <package name="manager" extends="struts-default"> 12 13 <action name="upload" class="com.wishwzp.action.FileUploadAction"> 14 <result name="success">/success.jsp</result> 15 <result name="input">/fileupload.jsp</result> 16 17 </action> 18 19 </package> 20 21 </struts>
第四节:多文件上传
strutsl.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <package name="manager" extends="struts-default"> 8 <action name="uploads" class="com.wishwzp.action.FilesUploadAction"> 9 <result name="success">/success.jsp</result> 10 <result name="input">/filesupload.jsp</result> 11 </action> 12 13 14 </package> 15 16 </struts>
FilesUploadAction.java
1 package com.wishwzp.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 FilesUploadAction extends ActionSupport{ 10 11 /** 12 * 13 */ 14 private static final long serialVersionUID = 1L; 15 16 // 文件(这里的文件名必须要和前端的文件:<input type="file" name="wishwzp"/><br/>name一样) 17 private File[] wishwzp; 18 19 // 文件名(name+FileName) 20 private String[] wishwzpFileName; 21 22 // 文件类型(name+ContentType) 23 private String[] wishwzpContentType; 24 25 26 public File[] getwishwzp() { 27 return wishwzp; 28 } 29 30 31 public void setwishwzp(File[] wishwzp) { 32 this.wishwzp = wishwzp; 33 } 34 35 36 public String[] getwishwzpFileName() { 37 return wishwzpFileName; 38 } 39 40 41 public void setwishwzpFileName(String[] wishwzpFileName) { 42 this.wishwzpFileName = wishwzpFileName; 43 } 44 45 46 public String[] getwishwzpContentType() { 47 return wishwzpContentType; 48 } 49 50 51 public void setwishwzpContentType(String[] wishwzpContentType) { 52 this.wishwzpContentType = wishwzpContentType; 53 } 54 55 56 @Override 57 public String execute() throws Exception { 58 for(int i=0;i<wishwzp.length;i++){ 59 System.out.println("文件名:"+wishwzpFileName[i]); 60 System.out.println("文件类型:"+wishwzpContentType[i]); 61 //上传到C盘中 62 String filePath="C:/"+wishwzpFileName[i]; 63 //保存文件添加到文件中 64 File saveFile=new File(filePath); 65 //将wishwzp文件拷到saveFile中,这样数据就到C盘中了 66 FileUtils.copyFile(wishwzp[i], saveFile); 67 } 68 return SUCCESS; 69 } 70 71 }
filesupload.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <s:fielderror></s:fielderror> 12 <form action="uploads" method="post" enctype="multipart/form-data"> 13 文件1:<input type="file" name="wishwzp"/><br/> 14 文件2:<input type="file" name="wishwzp"/><br/> 15 文件3:<input type="file" name="wishwzp"/><br/> 16 <input type="submit" value="提交"/> 17 </form> 18 </body> 19 </html>
第五节:Struts2 文件下载
返回的是文件流
<param name="contentDisposition">attachment;filename=${fileName}</param>
InputStream getInputStream()
struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 <constant name="struts.multipart.maxSize" value="20000000"></constant> 9 10 <package name="manager" extends="struts-default"> 11 <action name="download" class="com.wishwzp.action.FileDownloadAction"> 12 <result type="stream"> 13 <param name="contentDisposition">attachment;filename=${fileName}</param> 14 </result> 15 </action> 16 17 </package> 18 19 </struts>
FileDownloadAction.java
1 package com.wishwzp.action; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.InputStream; 6 7 import com.opensymphony.xwork2.ActionSupport; 8 9 public class FileDownloadAction extends ActionSupport{ 10 11 /** 12 * 13 */ 14 private static final long serialVersionUID = 1L; 15 16 private String fileName; 17 18 public String getFileName() throws Exception{ 19 fileName=new String(fileName.getBytes(),"ISO8859-1"); 20 return fileName; 21 } 22 23 public void setFileName(String fileName) { 24 this.fileName = fileName; 25 } 26 27 public InputStream getInputStream()throws Exception{ 28 File file=new File("C:/美女1.jpg"); 29 this.fileName="美女1号"; 30 return new FileInputStream(file); 31 } 32 33 }
filedownload.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <a href="download">文件下载</a> 12 </body> 13 </html>