Struts2之文件上传(单文件/多文件)

<一>简述:

Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>

<二>指定用户上传文件的大小,有两种方式:

1)默认是在default.properties 文件的 struts.multipart.maxSize=2097152  键值指定为2097152 也就是2M,通过计算 2097152/(1024*1024) = 2 M

那我们可以改变其默认值,只要在src目录下,新建一个 struts.properties 文件,指定上传大小 如下:

一次上传只可以上传10M,不管一次上传多少个文件,按总和计算

2)在struts.xml文件中指定,如图:

其实name就对应struts.properties的键,value对应 值

注意:如果即在struts.properties设定文件上传大小,又在struts.xml 设定文件上传大小,则struts.properties的优先级高于struts.xml,一般在一处指定上传大小即可,推荐 struts.properties

<三>Struts2之单文件上传

1.fileupload.jsp

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'fileupload.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.      <!-- enctype 默认是 application/x-www-form-urlencoded -->  
  27.      <form action="FileUpload2" enctype="multipart/form-data" method="post" >  
  28.           
  29.                用户名:<input type="text" name="usename"<br/>  
  30.                上传文件:<input type="file" name="file1"><br/>  
  31.                  
  32.               <input type="submit" value="提交"/>  
  33.        
  34.      </form>  
  35.        
  36.        
  37.        
  38.   </body>  
  39. </html>  


2.具体处理上传的 FileUpload.java

[java] view plain copy
  1. package com.struts2.fileupload;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8.   
  9. import org.apache.struts2.ServletActionContext;  
  10.   
  11. import com.opensymphony.xwork2.ActionSupport;  
  12.   
  13. /** 
  14.  * 单个文件上传 
  15.  * @author Administrator 
  16.  * 上传文件其实是上传了两份, 
  17.  *  
  18.  * 首先将上传的文件保存到 default.properties 文件中 struts.multipart.saveDir键指定的目录中 
  19.  * 默认是空的 
  20.  * 保存在  Tomcat 6.0workCatalinalocalhoststruts2目录下以.tmp后缀名的文件 
  21.  *  
  22.  * 如果要在 struts.multipart.saveDir 指定目录, 则可以在 src文件夹下 建一个 struts.properties, 
  23.  * 覆盖  default.properties 的某些键值 
  24.  *  
  25.  * 还有一份是 存放在自己设定的目录下 
  26.  */  
  27. public class FileUpload extends ActionSupport {  
  28.       
  29.     private String usename ;  
  30.     private File file1 ; //具体上传文件的 引用 , 指向临时目录中的临时文件  
  31.     private String file1FileName ;  // 上传文件的名字 ,FileName 固定的写法  
  32.     private String file1ContentType ; //上传文件的类型, ContentType 固定的写法  
  33.       
  34.     public String getUsename() {  
  35.         return usename;  
  36.     }  
  37.     public void setUsename(String usename) {  
  38.         this.usename = usename;  
  39.     }  
  40.     public File getFile1() {  
  41.         return file1;  
  42.     }  
  43.     public void setFile1(File file1) {  
  44.         this.file1 = file1;  
  45.     }  
  46.     public String getFile1FileName() {  
  47.         return file1FileName;  
  48.     }  
  49.     public void setFile1FileName(String file1FileName) {  
  50.         this.file1FileName = file1FileName;  
  51.     }  
  52.     public String getFile1ContentType() {  
  53.         return file1ContentType;  
  54.     }  
  55.     public void setFile1ContentType(String file1ContentType) {  
  56.         this.file1ContentType = file1ContentType;  
  57.     }  
  58.       
  59.     @Override  
  60.     public String execute() throws Exception {  
  61.         //获取文件存储路径  
  62.         String path = ServletActionContext.getRequest().getRealPath("/upload");  
  63.         //输出流  
  64.         OutputStream os = new FileOutputStream(new File(path,file1FileName));  
  65.         //输入流  
  66.         InputStream is = new FileInputStream(file1);  
  67.           
  68.         byte[] buf = new byte[1024];  
  69.         int length = 0 ;  
  70.           
  71.         while(-1 != (length = is.read(buf) ) )  
  72.         {  
  73.             os.write(buf, 0, length) ;  
  74.         }  
  75.         is.close();  
  76.         os.close();  
  77.           
  78.         return SUCCESS;  
  79.     }  
  80.       
  81.       
  82.       
  83.   
  84. }  


3.最终显示结果的页面,filedemo.jsp

[java] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3.   
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.   <head>  
  12.     <base href="<%=basePath%>">  
  13.       
  14.     <title>My JSP 'filedemo.jsp' starting page</title>  
  15.       
  16.     <meta http-equiv="pragma" content="no-cache">  
  17.     <meta http-equiv="cache-control" content="no-cache">  
  18.     <meta http-equiv="expires" content="0">      
  19.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  20.     <meta http-equiv="description" content="This is my page">  
  21.     <!--  
  22.     <link rel="stylesheet" type="text/css" href="styles.css">  
  23.     -->  
  24.   
  25.   </head>  
  26.     
  27.   <body>  
  28.     上传成功: <br/>  
  29.     usename: <s:property value="usename" /><br/>  
  30.     file: <s:property value="file1FileName"/><br/>  
  31.     contentType: <s:property value="file1ContentType"/>  
  32.       
  33.   </body>  
  34. </html>  



<四>Struts2之多文件上传

1.fileupload.jsp

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'fileupload.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.      <!-- enctype 默认是 application/x-www-form-urlencoded -->  
  27.      <form action="FileUpload2" enctype="multipart/form-data" method="post" >  
  28.           
  29.                用户名:<input type="text" name="usename"<br/>  
  30.                上传文件:<input type="file" name="file1"><br/>  
  31.                上传文件: <input type="file" name="file1"><br/> <!-- 两个名字相同 都是file1 -->  
  32.               <input type="submit" value="提交"/>  
  33.        
  34.      </form>  
  35.        
  36.        
  37.        
  38.   </body>  
  39. </html>  


两个上传文件的name属性值要是一样的,后台方便处理

2.具体处理上传文件的FileUpload2.java

多文件上传用集合的方式

[java] view plain copy
  1. package com.struts2.fileupload;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import java.util.List;  
  9.   
  10. import org.apache.struts2.ServletActionContext;  
  11.   
  12. import com.opensymphony.xwork2.ActionSupport;  
  13.   
  14. /** 
  15.  * 多文件上传,用集合的方式 
  16.  * @author Administrator 
  17.  * 
  18.  */  
  19.   
  20. public class FileUpload2 extends ActionSupport {  
  21.       
  22.     private String usename ;  
  23.     private List<File> file1 ;  
  24.     private List<String> file1FileName ;  
  25.     private List<String> file1ContentType ;  
  26.       
  27.     public String getUsename() {  
  28.         return usename;  
  29.     }  
  30.     public void setUsename(String usename) {  
  31.         this.usename = usename;  
  32.     }  
  33.     public List<File> getFile1() {  
  34.         return file1;  
  35.     }  
  36.     public void setFile1(List<File> file1) {  
  37.         this.file1 = file1;  
  38.     }  
  39.     public List<String> getFile1FileName() {  
  40.         return file1FileName;  
  41.     }  
  42.     public void setFile1FileName(List<String> file1FileName) {  
  43.         this.file1FileName = file1FileName;  
  44.     }  
  45.     public List<String> getFile1ContentType() {  
  46.         return file1ContentType;  
  47.     }  
  48.     public void setFile1ContentType(List<String> file1ContentType) {  
  49.         this.file1ContentType = file1ContentType;  
  50.     }  
  51.       
  52.     @Override  
  53.     public String execute() throws Exception {  
  54.           
  55.         //获取文件存储路径  
  56.         String path = ServletActionContext.getRequest().getRealPath("/upload");  
  57.           
  58.         for(int i = 0 ; i < file1.size() ; i++ )  
  59.         {  
  60.             OutputStream os = new FileOutputStream(new File(path,file1FileName.get(i)));  
  61.               
  62.             InputStream is = new FileInputStream(file1.get(i));  
  63.               
  64.             byte[] buf = new byte[1024];  
  65.             int length = 0 ;  
  66.               
  67.             while(-1 != (length = is.read(buf) ) )  
  68.             {  
  69.                 os.write(buf, 0, length) ;  
  70.             }  
  71.               
  72.             is.close();  
  73.             os.close();  
  74.               
  75.         }  
  76.           
  77.         return SUCCESS;  
  78.     }  
  79.   
  80. }  


3.用于显示的界面filedemo.jsp

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3.   
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.   <head>  
  12.     <base href="<%=basePath%>">  
  13.       
  14.     <title>My JSP 'filedemo2.jsp' starting page</title>  
  15.       
  16.     <meta http-equiv="pragma" content="no-cache">  
  17.     <meta http-equiv="cache-control" content="no-cache">  
  18.     <meta http-equiv="expires" content="0">      
  19.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  20.     <meta http-equiv="description" content="This is my page">  
  21.     <!-- 
  22.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  23.     -->  
  24.   
  25.   </head>  
  26.     
  27.   <body>  
  28.               上传成功:<br/>  
  29.      usename:<s:property value="usename"/><br/>  
  30.               <!-- 遍历值 -->  
  31.               <s:iterator value="file1FileName" id="f"<!-- id是一个对象,目前是一个字符串集合  可任意命名 -->  
  32.                                                  文件:<s:property value="#f"/> <br/>    
  33.                <!-- 这里也可以调用方法  <s:property value="#f.toUpperCase()"/> -->  
  34.               </s:iterator>  
  35.       
  36.       
  37.   </body>  
  38. </html>  


遍历集合的方式,用struts2提供的标签 iterator 可以实现

              <s:iterator value="file1FileName" id="f"> <!-- id是一个对象,目前是一个字符串集合  可任意命名-->                                                  文件:<s:property value="#f"/> <br/>                  <!-- 这里也可以调用方法  <s:property value="#f.toUpperCase()"/> -->

               toUpperCase()字符串的方法是把字母转为大写               </s:iterator>

原文地址:https://www.cnblogs.com/hoobey/p/5294321.html