天天看点

struts文件上传详解 Struts2上传单文件或者多个文件的好处

Struts2上传单文件或者多个文件的好处

问题?Struts2上传单文件或者多个文件的好处在哪里呢?我们都知道上传文件的基本步骤都是读取文件域,得到文件名,然后得到真实存储路径,然后再构建输入输出流,这样几个步骤,我们的文件上传也就搭成了。显得有些麻烦了是吧?接下来让我们去看看,struts2给我们带来的便利。

原理:struts2拥有自动转换类型的功能,这是好处一。struts2我们所拷入的common-io.jar包集成了一个工具类,运用这个工具类可以帮我们达到上传文件的目的。

*首先搭建好struts2的环境(7个jar包,拦截器之类的配置),详情可看:http://blog.csdn.net/mr_li13/article/details/49391329?

案例:运用struts2肯定要用它的配置文件struts.xml啦、

[html]  view plain copy print ?

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.             "-//Apache Software Foundation//DTD Struts Configuration <span style="color:#ff0000;">2.0</span>//EN"  
  4.             "http://struts.apache.org/dtds/struts-<span style="color:#ff0000;">2.0</span>.dtd">  
  5. <struts>  
  6. <!-- constant表示常量设置,设置配置文件自动更新,开发中很重要 -->  
  7. <constant name="struts.action.extension" value="action,,do"></constant>  
  8. <constant name="struts.devMode" value="true"></constant>  
  9. <!-- 各个配置文件的包含 -->  
  10. <!-- 单个文件下载action -->  
  11. <include file="UpLoadFile.xml"></include>  
  12. <!-- 多个文件上传action -->  
  13. <include file="moreUpLoadFile.xml"></include>  
  14. </struts>  

红色标记处。一定要相同。不然就会出不联网就会报错。详情请看

1.单个文件:

UpLoadFile.xml文件内容

[html]  view plain copy print ?

  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. <struts>  
  6. <!-- constant表示常量设置,设置配置文件自动更新,开发中很重要 -->  
  7. <constant name="struts.action.extension" value="action,,do"></constant>  
  8. <constant name="struts.devMode" value="true"></constant>  
  9. <!-- 设置文件上传大小 50M-->  
  10. <constant name="struts.multipart.maxSize" value="52428800"></constant>  
  11.  <!-- 文件下载action -->  
  12. <package name="upload" namespace="/upload" extends="struts-default">  
  13.     <action name="upload1" class="com.itcast.web.domain.uploadAction" method="execute">  
  14.         <result name="success">/uploadSuccess.jsp</result>  
  15.     </action>  
  16. </package>  
  17. </struts>  

上传页面:upload.jsp

[html]  view plain copy print ?

  1. <!-- 于http://localhost:8080/Struts2day1/upload.jsp -->  
  2.     <form action="${pageContext.request.contextPath}/upload/upload1.action" method="post" enctype="multipart/form-data">  
  3.         文件:<input type="file" name="img"/><br/><!-- name这个名字必须和action类中的setter名字相同,首先执行setter方法,struts2会自动封装 -->  
  4.             <input type="submit" value="上传"/>  
  5.     </form>  

action实体类:uploadAction.class

[java]  view plain copy print ?

  1. package com.itcast.web.domain;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import java.io.Serializable;  
  9. import javax.servlet.ServletContext;  
  10. import org.apache.commons.io.FileUtils;  
  11. import org.apache.struts2.ServletActionContext;  
  12. import com.opensymphony.xwork2.ActionContext;  
  13. import com.opensymphony.xwork2.ActionSupport;  
  14. public class uploadAction extends ActionSupport implements Serializable {  
  15.     private File img;//代表表单中文件上传输入域的名称,struts会自动封装成file域在此进行处理  
  16.     private String imgFileName;  
  17.     //得到上传域中,文件的名字,是不是很方便啊,struts已经为我们做好了  
  18.     //只需要与File的对象名称多FileName就可以了,而且文件名的编码格式已经转化好了  
  19.     private String imgContentType;//同理封装好了一个文件类型类  
  20.     public File getImg() {  
  21.         return img;  
  22.     }  
  23.     public void setImg(File img) {  
  24.         this.img = img;  
  25.     }  
  26.     public String getImgFileName() {  
  27.         return imgFileName;  
  28.     }  
  29.     public void setImgFileName(String imgFileName) {  
  30.         this.imgFileName = imgFileName;  
  31.     }  
  32.     public String getImgContentType() {  
  33.         return imgContentType;  
  34.     }  
  35.     public void setImgContentType(String imgContentType) {  
  36.         this.imgContentType = imgContentType;  
  37.     }  
  38.     public String execute(){  
  39. //      我们自然用最新的方式。简便许多  
  40.         try {  
  41.                     System.out.println(imgContentType);//image/jpeg  
  42.             //找到文件存放路径  
  43.             ServletContext sc=ServletActionContext.getServletContext();  
  44.             String storpath=sc.getRealPath("files");  
  45.             //老方式  
  46.             //构建输出流  
  47. //          OutputStream out=new FileOutputStream(storpath+"\\"+imgFileName);  
  48. //          InputStream in=new FileInputStream(img);  
  49. //            
  50. //          byte b[]=new byte[1024];  
  51. //          int len=-1;  
  52. //          while((len=in.read(b))!=-1){  
  53. //              out.write(b, 0, len);  
  54. //          }  
  55. //          out.close();  
  56. //          in.close();  
  57.             //新方式,用拷入的jar包commons-io这个jar包,  
  58.             //里面有封装的一个类,使我们上传文件更容易(前者是文件源,后者是路径(主路径,子路径))  
  59.             FileUtils.copyFile(img, new File(storpath, imgFileName));  
  60.             ActionContext.getContext().put("message", "上传成功!");  
  61.             return SUCCESS;  
  62.         } catch (Exception e) {  
  63.             e.printStackTrace();  
  64.             return ERROR;  
  65.         }  
  66.     }  
  67. }  

成功页面:uploadSuccess.jsp

[html]  view plain copy print ?

  1. <body>  
  2.     ${message }  
  3.   </body>  

2.多文件上传

其实方式都差不多只是将实体类中的属性变成了数组形式

moreUploadFile.xml

[html]  view plain copy print ?

  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. <struts>  
  6. <!-- constant表示常量设置,设置配置文件自动更新,开发中很重要 -->  
  7. <constant name="struts.action.extension" value="action,,do"></constant>  
  8. <constant name="struts.devMode" value="true"></constant>  
  9. <!-- 设置文件上传大小 50M-->  
  10. <constant name="struts.multipart.maxSize" value="52428800"></constant>  
  11. <package name="moreupload" namespace="/moreupload" extends="struts-default">  
  12.     <action name="upload1" class="com.itcast.web.domain.moreUploadAction" method="execute">  
  13.         <result name="success">/uploadSuccess.jsp</result>  
  14.     </action>  
  15. </package>  
  16. </struts>  

moreupload.jsp

[html]  view plain copy print ?

  1. <!-- http://localhost:8080/Struts2day1/upload.jsp -->  
  2.     <form action="${pageContext.request.contextPath}/moreupload/upload1.action" method="post" enctype="multipart/form-data">  
  3.         文件1:<input type="file" name="img"/><br/><!-- name这个名字必须和action类中的setter名字相同,首先执行setter方法,struts2会自动封装 -->  
  4.         文件2:<input type="file" name="img"/><br/>  
  5.             <input type="submit" value="上传"/>  
  6.     </form>  

实体类:moreUploadAction.class

[java]  view plain copy print ?

  1. package com.itcast.web.domain;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import java.io.Serializable;  
  9. import javax.servlet.ServletContext;  
  10. import org.apache.commons.io.FileUtils;  
  11. import org.apache.struts2.ServletActionContext;  
  12. import com.opensymphony.xwork2.ActionContext;  
  13. import com.opensymphony.xwork2.ActionSupport;  
  14. public class moreUploadAction extends ActionSupport implements Serializable {  
  15.     private File[] img;//代表表单中文件上传输入域的名称,struts会自动封装成file域在此进行处理  
  16.     private String[] imgFileName;  
  17.     //得到上传域中,文件的名字,是不是很方便啊,struts已经为我们做好了  
  18.     //只需要与File的对象名称多FileName就可以了,而且文件名的编码格式已经转化好了  
  19.     private String[] imgContentType;//同理封装好了一个文件类型类  
  20.     public File[] getImg() {  
  21.         return img;  
  22.     }  
  23.     public void setImg(File[] img) {  
  24.         this.img = img;  
  25.     }  
  26.     public String[] getImgFileName() {  
  27.         return imgFileName;  
  28.     }  
  29.     public void setImgFileName(String[] imgFileName) {  
  30.         this.imgFileName = imgFileName;  
  31.     }  
  32.     public String[] getImgContentType() {  
  33.         return imgContentType;  
  34.     }  
  35.     public void setImgContentType(String[] imgContentType) {  
  36.         this.imgContentType = imgContentType;  
  37.     }  
  38.     public String  execute(){  
  39.         try {  
  40.             if(img!=null&&img.length>0){  
  41.             ServletContext sc=ServletActionContext.getServletContext();  
  42.             String storpath=sc.getRealPath("files");  
  43.             for(int i=0;i<img.length;i++){  
  44.                     FileUtils.copyFile(img[i], new File(storpath, imgFileName[i]));  
  45.                 }  
  46.             }  
  47.             ActionContext.getContext().put("message", "上传成功!");  
  48.             return SUCCESS;  
  49.         } catch (Exception e) {  
  50.             e.printStackTrace();  
  51.             return ERROR;  
  52.             }  
  53.     }  
  54. }  

成功页面和uploadSuccess.jso内容一样的,共用。

继续阅读