天天看點

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内容一樣的,共用。

繼續閱讀