(十)struts2之文件上傳

自接觸這麼多種技術的上傳來看,還是Struts2的上傳最好用,雖然之前有篇文章已經總結了幾乎我接觸到的所有類型的上傳,但Struts2方面感覺講的還是不夠細緻。

本文就單文件上傳和批量文件上傳來進行講解

具體示例

首頁上傳頁面


Html代碼  收藏代碼
  1. <span style="font-size: large;"><span style="font-size: large;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="/struts-tags" prefix="s" %>  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4. <html>  
  5.   <head>  
  6.     <title>Struts2文件上傳示例</title>  
  7.     <meta http-equiv="pragma" content="no-cache">  
  8.     <meta http-equiv="cache-control" content="no-cache">  
  9.     <meta http-equiv="expires" content="0">      
  10.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  11.     <meta http-equiv="description" content="This is my page">  
  12.     <!-- 
  13.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  14.     -->  
  15.   </head>  
  16.     
  17.   <body>  
  18.     <h3>Struts2文件上傳示例</h3><hr/>  
  19.       
  20.     <form action="fileUpload.action" method="post" enctype="multipart/form-data">  
  21.         <input type="file" name="up" style="width: 500px"/> <s:fielderror name="up"></s:fielderror><br/>  
  22.         <input type="submit" value="開始上傳"/>  
  23.         <s:token/>  
  24.     </form>  
  25.       
  26.     <h3>Struts2批量文件上傳示例</h3><hr/>  
  27.       
  28.     <form action="batchFileUpload.action" method="post" enctype="multipart/form-data">  
  29.     <s:fielderror name="up"></s:fielderror><br/>  
  30.         <input type="file" name="up" style="width: 500px"/> <br/>  
  31.         <input type="file" name="up" style="width: 500px"/><br/>  
  32.         <input type="file" name="up" style="width: 500px"/> <br/>  
  33.         <input type="submit" value="開始上傳"/>  
  34.         <s:token/>  
  35.     </form>  
  36.       
  37.   </body>  
  38. </html></span></span>  


 單文件上傳的Action


Java代碼  收藏代碼
  1. <span style="font-size: large;"><span style="font-size: large;">package com.javacrazyer.action;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9.   
  10. import org.apache.struts2.ServletActionContext;  
  11.   
  12. import com.opensymphony.xwork2.ActionSupport;  
  13.   
  14.   
  15. public class FileUploadAction extends ActionSupport {  
  16.       
  17.     private static final long serialVersionUID = -7001482935770262132L;  
  18.     private File up;   
  19.     private String upContentType;  
  20.     private String upFileName;  
  21.     private String destPath;  
  22.   
  23.     public String execute() throws Exception{  
  24.           
  25.         System.out.println("contentType==" + this.upContentType);  
  26.         System.out.println("fileName==" + this.upFileName);  
  27.           
  28.         File basePath = new File(ServletActionContext.getServletContext().getRealPath(destPath));  
  29.           
  30.         if(!basePath.exists()){  
  31.             basePath.mkdirs();  
  32.         }  
  33.           
  34.         copy(up, new File(basePath.getCanonicalPath()+"/" + upFileName));  
  35.           
  36.         return SUCCESS;  
  37.     }  
  38.   
  39.       
  40.     public void copy(File srcFile, File destFile) throws IOException{  
  41.         BufferedInputStream bis = null;  
  42.         BufferedOutputStream bos = null;  
  43.           
  44.         try{  
  45.             bis = new BufferedInputStream(new FileInputStream(srcFile));  
  46.             bos = new BufferedOutputStream(new FileOutputStream(destFile));  
  47.             byte[] buf = new byte[8192];  
  48.               
  49.             for(int count = -1; (count = bis.read(buf))!= -1; ){  
  50.                 bos.write(buf, 0, count);  
  51.             }  
  52.             bos.flush();  
  53.         }catch(IOException ie){  
  54.             throw ie;  
  55.         }finally{  
  56.             if(bis != null){  
  57.                 bis.close();  
  58.             }  
  59.             if(bos != null){  
  60.                 bos.close();  
  61.             }  
  62.         }  
  63.     }  
  64.       
  65.       
  66.   
  67.     public File getUp() {  
  68.         return up;  
  69.     }  
  70.   
  71.   
  72.     public void setUp(File up) {  
  73.         this.up = up;  
  74.     }  
  75.   
  76.   
  77.     public String getUpContentType() {  
  78.         return upContentType;  
  79.     }  
  80.   
  81.   
  82.     public void setUpContentType(String upContentType) {  
  83.         this.upContentType = upContentType;  
  84.     }  
  85.   
  86.   
  87.     public String getUpFileName() {  
  88.         return upFileName;  
  89.     }  
  90.   
  91.   
  92.     public void setUpFileName(String upFileName) {  
  93.         this.upFileName = upFileName;  
  94.     }  
  95.   
  96.   
  97.     public String getDestPath() {  
  98.         return destPath;  
  99.     }  
  100.   
  101.     public void setDestPath(String destPath) {  
  102.         this.destPath = destPath;  
  103.     }  
  104.       
  105. }</span></span>  


 多文件上傳的Action


Java代碼  收藏代碼
  1. <span style="font-size: large;"><span style="font-size: large;">package com.javacrazyer.action;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.util.ArrayList;  
  10. import java.util.List;  
  11.   
  12. import org.apache.struts2.ServletActionContext;  
  13.   
  14. import com.opensymphony.xwork2.ActionSupport;  
  15.   
  16. /** 
  17.  * 批量文件上傳 
  18.  *  
  19.  * 這裏有一點非常重要:就是凡是頁面上的file文本域的name=xxx的, 
  20.  * 那麼Action的三個屬性必須爲xxx,xxxContentType,xxxFileName 
  21.  *  
  22.  *  
  23.  */  
  24. public class BatchFileUploadAction extends ActionSupport {  
  25.       
  26.     private static final long serialVersionUID = -7001482935770262132L;  
  27.       
  28.     private List<File> up;   
  29.     private List<String> upContentType;  
  30.     private List<String> upFileName;  
  31.     private String destPath;  
  32.   
  33.     public String execute() throws Exception{  
  34.           
  35.         //System.out.println("contentType==" + this.upContentType);  
  36.         //System.out.println("fileName==" + this.upFileName);  
  37.           
  38.         File basePath = new File(ServletActionContext.getServletContext().getRealPath(destPath));  
  39.           
  40.         if(!basePath.exists()){  
  41.             basePath.mkdirs();  
  42.         }  
  43.           
  44.         int size = up == null ? 0 : up.size();  
  45.         for(int i = 0; i < size; i++){  
  46.             /* 
  47.              * 如果需要限制上傳文件的類型,那麼可以解開此註釋 
  48.              * if(isAllowType(upContentType.get(i))){ 
  49.                 copy(up.get(i), new File(basePath.getCanonicalPath()+"/" + upFileName.get(i))); 
  50.             }else{ 
  51.                 upFileName.remove(i); 
  52.             }*/  
  53.             copy(up.get(i), new File(basePath.getCanonicalPath()+"/" + upFileName.get(i)));  
  54.         }  
  55.         return SUCCESS;  
  56.     }  
  57.   
  58.       
  59.     public void copy(File srcFile, File destFile) throws IOException{  
  60.         BufferedInputStream bis = null;  
  61.         BufferedOutputStream bos = null;  
  62.           
  63.         try{  
  64.             bis = new BufferedInputStream(new FileInputStream(srcFile));  
  65.             bos = new BufferedOutputStream(new FileOutputStream(destFile));  
  66.             byte[] buf = new byte[8192];  
  67.               
  68.             for(int count = -1; (count = bis.read(buf))!= -1; ){  
  69.                 bos.write(buf, 0, count);  
  70.             }  
  71.             bos.flush();  
  72.         }catch(IOException ie){  
  73.             throw ie;  
  74.         }finally{  
  75.             if(bis != null){  
  76.                 bis.close();  
  77.             }  
  78.             if(bos != null){  
  79.                 bos.close();  
  80.             }  
  81.         }  
  82.     }  
  83.   
  84.   
  85.     public List<File> getUp() {  
  86.         return up;  
  87.     }  
  88.   
  89.   
  90.     public void setUp(List<File> up) {  
  91.         this.up = up;  
  92.     }  
  93.   
  94.   
  95.     public List<String> getUpContentType() {  
  96.         return upContentType;  
  97.     }  
  98.   
  99.   
  100.     public void setUpContentType(List<String> upContentType) {  
  101.         this.upContentType = upContentType;  
  102.     }  
  103.   
  104.   
  105.     public List<String> getUpFileName() {  
  106.         return upFileName;  
  107.     }  
  108.   
  109.   
  110.     public void setUpFileName(List<String> upFileName) {  
  111.         this.upFileName = upFileName;  
  112.     }  
  113.   
  114.   
  115.     public String getDestPath() {  
  116.         return destPath;  
  117.     }  
  118.   
  119.     public void setDestPath(String destPath) {  
  120.         this.destPath = destPath;  
  121.     }  
  122.   
  123.     public static boolean isAllowType(String type){  
  124.         List<String> types = new ArrayList<String>();  
  125.         types.add("image/pjpeg");  
  126.         types.add("text/plain");  
  127.         types.add("image/gif");  
  128.         types.add("image/x-png");  
  129.           
  130.         if(types.contains(type)){  
  131.             return true;  
  132.         }else{  
  133.             return false;  
  134.         }  
  135.     }  
  136. }</span></span>  

針對上面兩個Action,都得有那麼三個屬性【上傳的文件,文件類型,文件名】,並且開頭必須與表單file的name值一樣



在Action中添加一個List<File>類型的與頁面所有file域同名的屬性。private List<File> up;

 添加一個以file域名開頭,後面跟ContentType的字符串列表屬性,這個由Struts2的文件上傳攔截器賦文件類型值。如:private List<String> upContentTyp;

        添加一個以file域名開頭,後面跟FileName的字符串列表屬性,這個由Struts2的文件上傳攔截器賦文件名的值。如:private List<String> upFileName;

       通過IO流循環操作,完成文件的讀寫。


記住:在struts2的Action中,對於無論是單個文件上傳還是批量上傳,就是凡是頁面上的file文本域的name=xxx的, 那麼Action的三個屬性必須爲xxx,xxxContentType,xxxFileName



 存放上傳類型錯誤信息的資源文件

 msg_zh_CN.properties



Xml代碼  收藏代碼
  1. <span style="font-size: large;"><span style="font-size: large;">struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u7684\u6587\u4EF6\u7C7B\u578B\u662F\u975E\u6CD5\u7684  
  2. #struts.messages.error.uploading=文件不能上傳的通用錯誤信息  
  3. #struts.messages.error.file.too.large=上傳文件長度過大的錯誤信息  
  4. #struts.messages.error.content.type.not.allowed= 當上傳文件不符合指定的contentType</span></span>  

 




 

msg_en_US.properties


Html代碼  收藏代碼
  1. <span style="font-size: large;"><span style="font-size: large;">struts.messages.error.content.type.not.allowed=upload file contenttype is invalidate</span></span>  


src/struts.xml


Xml代碼  收藏代碼
  1. <span style="font-size: large;"><span style="font-size: large;"><?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.1.7.dtd">  
  5. <struts>  
  6.     <!-- 請求參數的編碼方式 -->  
  7.     <constant name="struts.i18n.encoding" value="UTF-8"/>  
  8.     <!-- 指定被struts2處理的請求後綴類型。多個用逗號隔開 -->  
  9.     <constant name="struts.action.extension" value="action,do,go,xkk"/>  
  10.     <!-- 當struts.xml改動後,是否重新加載。默認值爲false(生產環境下使用),開發階段最好打開  -->  
  11.     <constant name="struts.configuration.xml.reload" value="true"/>  
  12.     <!-- 是否使用struts的開發模式。開發模式會有更多的調試信息。默認值爲false(生產環境下使用),開發階段最好打開  -->  
  13.     <constant name="struts.devMode" value="false"/>  
  14.     <!-- 設置瀏覽器是否緩存靜態內容。默認值爲true(生產環境下使用),開發階段最好關閉  -->  
  15.     <constant name="struts.serve.static.browserCache" value="false" />  
  16.     <!-- 是否允許在OGNL表達式中調用靜態方法,默認值爲false -->  
  17.     <constant name="struts.ognl.allowStaticMethodAccess" value="true"/>  
  18.     <!-- 指定由spring負責action對象的創建   
  19.     <constant name="struts.objectFactory" value="spring" />-->  
  20.       
  21.     <constant name="struts.enable.DynamicMethodInvocation" value="false"/>  
  22.     <constant name="struts.multipart.maxSize" value="102400000000000" />   
  23.       
  24.     <constant name="struts.custom.i18n.resources" value="msg"/>  
  25.        
  26.     <package name="my" extends="struts-default" namespace="/">  
  27.     <interceptors>  
  28.             <interceptor-stack name="myStack">  
  29.                 <interceptor-ref name="defaultStack"/>  
  30.                 <interceptor-ref name="token"/>  
  31.             </interceptor-stack>  
  32.         </interceptors>  
  33.         <default-interceptor-ref name="myStack"/>  
  34.           
  35.         <global-results>  
  36.             <result name="invalid.token">/error.jsp</result>  
  37.         </global-results>  
  38.           
  39.         <action name="fileUpload" class="com.javacrazyer.action.FileUploadAction">  
  40.             <param name="destPath">/abc</param>  
  41.             <result name="input">/index.jsp</result>  
  42.             <result>/success.jsp</result>  
  43.         </action>  
  44.         <action name="batchFileUpload" class="com.javacrazyer.action.BatchFileUploadAction">  
  45.             <param name="destPath">/abc</param>  
  46.             <result name="input">/index.jsp</result>  
  47.             <result>/success.jsp</result>  
  48.         </action>  
  49.           
  50.     </package>  
  51.       
  52. </struts></span></span>  

 



token防止重複提交表單【主要是防止上傳過後點擊瀏覽器的後退按鈕,再次提交這個問題】

  1) 在頁面的表單中添加<s:token/>

  2) 在要使用token的Action配置中添加token或tokenSession攔截器。


  

看到沒有,我們在配置中還配置了<result name="input">/index.jsp</result>,這樣的話,如果上傳失敗就會跳到index.jsp頁面,不過現在

我們還沒涉到類型限制的問題,接下來,將的就是如何爲上傳添加文件類型

struts2中爲文件上傳添加類型限制有兩種方式:

第一種方式在Action純寫代碼方式

看BatchFileUploadAction中,copy(up.get(i), new File(basePath.getCanonicalPath()+"/" + upFileName.get(i)));這句話,如果將其上邊的被註釋的段落註釋去掉,將本行註釋掉,那麼就可以起到文件限制,具體可以在isAllowType這個方法中配置

第二種方式在struts.xml中配置

將上邊struts.xml中的


Java代碼  收藏代碼
  1. <span style="font-size: large;"><span style="font-size: large;"><interceptors>  
  2.     <interceptor-stack name="myStack">  
  3.         <interceptor-ref name="defaultStack"/>  
  4.         <interceptor-ref name="token"/>  
  5.     </interceptor-stack>  
  6. </interceptors></span></span>  

 替換成下面的


Xml代碼  收藏代碼
  1. <span style="font-size: large;"><span style="font-size: large;">     <interceptors>  
  2.     struts2的defaultStack中已經含有fileupload攔截器,如果想加入allowedTypes參數,  
  3. 需要從新寫一個defaultstack ,拷貝過來修改一下  
  4.             <interceptor-stack name="myStack">  
  5.                 <interceptor-ref name="exception"/>  
  6.                 <interceptor-ref name="alias"/>  
  7.                 <interceptor-ref name="servletConfig"/>  
  8.                 <interceptor-ref name="i18n"/>  
  9.                 <interceptor-ref name="prepare"/>  
  10.                 <interceptor-ref name="chain"/>  
  11.                 <interceptor-ref name="debugging"/>  
  12.                 <interceptor-ref name="profiling"/>  
  13.                 <interceptor-ref name="scopedModelDriven"/>  
  14.                 <interceptor-ref name="modelDriven"/>  
  15.                    修改下面的這些文件類型值,就可以限制上傳文件的類型了  
  16.                 <interceptor-ref name="fileUpload">  
  17.                   <param name="allowedTypes">  
  18.                      image/png,image/gif,image/jpeg,text/plain  
  19.                   </param>  
  20.                 </interceptor-ref>  
  21.   上面配置的是上傳文件類型的限制,其實共有兩個參數  
  22. maximumSize (可選) - 這個攔截器允許的上傳到action中的文件最大長度(以byte爲單位).   
  23.                                                      注意這個參數和在webwork.properties中定義的屬性沒有關係,默認2MB  
  24.   
  25. allowedTypes (可選) - 以逗號分割的contentType類型列表(例如text/html),  
  26. 這些列表是這個攔截器允許的可以傳到action中的contentType.如果沒有指定就是允許任何上傳類型.     
  27.              
  28.                   
  29.                   
  30.                 <interceptor-ref name="checkbox"/>  
  31.                 <interceptor-ref name="staticParams"/>  
  32.                 <interceptor-ref name="actionMappingParams"/>  
  33.                 <interceptor-ref name="params">  
  34.                   <param name="excludeParams">dojo\..*,^struts\..*</param>  
  35.                 </interceptor-ref>  
  36.                 <interceptor-ref name="conversionError"/>  
  37.                 <interceptor-ref name="validation">  
  38.                     <param name="excludeMethods">input,back,cancel,browse</param>  
  39.                 </interceptor-ref>  
  40.                 <interceptor-ref name="workflow">  
  41.                     <param name="excludeMethods">input,back,cancel,browse</param>  
  42.                 </interceptor-ref>  
  43.                   Token,防止重複提交    
  44.                 <interceptor-ref name="token"/>  
  45.   
  46.             </interceptor-stack>  
  47. </interceptors></span></span>  

 那麼具體想添加什麼類型就可以在這個配置文件中 <interceptor-ref name="fileUpload">的內容裏進行自定義了

 現在爲止,開發中可能用到的類型大致有如下這麼多種,也就是mime類型

 

Java代碼  收藏代碼
  1. <span style="font-size: large;">application/vnd.lotus-1-2-3  
  2. 3gp video/3gpp  
  3. aab application/x-authoware-bin  
  4. aam application/x-authoware-map  
  5. aas application/x-authoware-seg  
  6. ai application/postscript  
  7. aif audio/x-aiff  
  8. aifc audio/x-aiff  
  9. aiff audio/x-aiff  
  10. als audio/X-Alpha5  
  11. amc application/x-mpeg  
  12. ani application/octet-stream  
  13. asc text/plain  
  14. asd application/astound  
  15. asf video/x-ms-asf  
  16. asn application/astound  
  17. asp application/x-asap  
  18. asx video/x-ms-asf  
  19. au audio/basic  
  20. avb application/octet-stream  
  21. avi video/x-msvideo  
  22. awb audio/amr-wb  
  23. bcpio application/x-bcpio  
  24. bin application/octet-stream  
  25. bld application/bld  
  26. bld2 application/bld2  
  27. bmp application/x-MS-bmp  
  28. bpk application/octet-stream  
  29. bz2 application/x-bzip2  
  30. cal image/x-cals  
  31. ccn application/x-cnc  
  32. cco application/x-cocoa  
  33. cdf application/x-netcdf  
  34. cgi magnus-internal/cgi  
  35. chat application/x-chat  
  36. class application/octet-stream  
  37. clp application/x-msclip  
  38. cmx application/x-cmx  
  39. co application/x-cult3d-object  
  40. cod image/cis-cod  
  41. cpio application/x-cpio  
  42. cpt application/mac-compactpro  
  43. crd application/x-mscardfile  
  44. csh application/x-csh  
  45. csm chemical/x-csml  
  46. csml chemical/x-csml  
  47. css text/css  
  48. cur application/octet-stream  
  49. dcm x-lml/x-evm  
  50. dcr application/x-director  
  51. dcx image/x-dcx  
  52. dhtml text/html  
  53. dir application/x-director  
  54. dll application/octet-stream  
  55. dmg application/octet-stream  
  56. dms application/octet-stream  
  57. doc application/msword  
  58. dot application/x-dot  
  59. dvi application/x-dvi  
  60. dwf drawing/x-dwf  
  61. dwg application/x-autocad  
  62. dxf application/x-autocad  
  63. dxr application/x-director  
  64. ebk application/x-expandedbook  
  65. emb chemical/x-embl-dl-nucleotide  
  66. embl chemical/x-embl-dl-nucleotide  
  67. eps application/postscript  
  68. eri image/x-eri  
  69. es audio/echospeech  
  70. esl audio/echospeech  
  71. etc application/x-earthtime  
  72. etx text/x-setext  
  73. evm x-lml/x-evm  
  74. evy application/x-envoy  
  75. exe application/octet-stream  
  76. fh4 image/x-freehand  
  77. fh5 image/x-freehand  
  78. fhc image/x-freehand  
  79. fif image/fif  
  80. fm application/x-maker  
  81. fpx image/x-fpx  
  82. fvi video/isivideo  
  83. gau chemical/x-gaussian-input  
  84. gca application/x-gca-compressed  
  85. gdb x-lml/x-gdb  
  86. gif image/gif  
  87. gps application/x-gps  
  88. gtar application/x-gtar  
  89. gz application/x-gzip  
  90. hdf application/x-hdf  
  91. hdm text/x-hdml  
  92. hdml text/x-hdml  
  93. hlp application/winhlp  
  94. hqx application/mac-binhex40  
  95. htm text/html  
  96. html text/html  
  97. hts text/html  
  98. ice x-conference/x-cooltalk  
  99. ico application/octet-stream  
  100. ief image/ief  
  101. ifm image/gif  
  102. ifs image/ifs  
  103. imy audio/melody  
  104. ins application/x-NET-Install  
  105. ips application/x-ipscript  
  106. ipx application/x-ipix  
  107. it audio/x-mod  
  108. itz audio/x-mod  
  109. ivr i-world/i-vrml  
  110. j2k image/j2k  
  111. jad text/vnd.sun.j2me.app-descriptor  
  112. jam application/x-jam  
  113. jar application/java-archive  
  114. jnlp application/x-java-jnlp-file  
  115. jpe image/jpeg  
  116. jpeg image/jpeg  
  117. jpg image/jpeg  
  118. jpz image/jpeg  
  119. js application/x-javascript  
  120. jwc application/jwc  
  121. kjx application/x-kjx  
  122. lak x-lml/x-lak  
  123. latex application/x-latex  
  124. lcc application/fastman  
  125. lcl application/x-digitalloca  
  126. lcr application/x-digitalloca  
  127. lgh application/lgh  
  128. lha application/octet-stream  
  129. lml x-lml/x-lml  
  130. lmlpack x-lml/x-lmlpack  
  131. lsf video/x-ms-asf  
  132. lsx video/x-ms-asf  
  133. lzh application/x-lzh  
  134. m13 application/x-msmediaview  
  135. m14 application/x-msmediaview  
  136. m15 audio/x-mod  
  137. m3u audio/x-mpegurl  
  138. m3url audio/x-mpegurl  
  139. ma1 audio/ma1  
  140. ma2 audio/ma2  
  141. ma3 audio/ma3  
  142. ma5 audio/ma5  
  143. man application/x-troff-man  
  144. map magnus-internal/imagemap  
  145. mbd application/mbedlet  
  146. mct application/x-mascot  
  147. mdb application/x-msaccess  
  148. mdz audio/x-mod  
  149. me application/x-troff-me  
  150. mel text/x-vmel  
  151. mi application/x-mif  
  152. mid audio/midi  
  153. midi audio/midi  
  154. mif application/x-mif  
  155. mil image/x-cals  
  156. mio audio/x-mio  
  157. mmf application/x-skt-lbs  
  158. mng video/x-mng  
  159. mny application/x-msmoney  
  160. moc application/x-mocha  
  161. mocha application/x-mocha  
  162. mod audio/x-mod  
  163. mof application/x-yumekara  
  164. mol chemical/x-mdl-molfile  
  165. mop chemical/x-mopac-input  
  166. mov video/quicktime  
  167. movie video/x-sgi-movie  
  168. mp2 audio/x-mpeg  
  169. mp3 audio/x-mpeg  
  170. mp4 video/mp4  
  171. mpc application/vnd.mpohun.certificate  
  172. mpe video/mpeg  
  173. mpeg video/mpeg  
  174. mpg video/mpeg  
  175. mpg4 video/mp4  
  176. mpga audio/mpeg  
  177. mpn application/vnd.mophun.application  
  178. mpp application/vnd.ms-project  
  179. mps application/x-mapserver  
  180. mrl text/x-mrml  
  181. mrm application/x-mrm  
  182. ms application/x-troff-ms  
  183. mts application/metastream  
  184. mtx application/metastream  
  185. mtz application/metastream  
  186. mzv application/metastream  
  187. nar application/zip  
  188. nbmp image/nbmp  
  189. nc application/x-netcdf  
  190. ndb x-lml/x-ndb  
  191. ndwn application/ndwn  
  192. nif application/x-nif  
  193. nmz application/x-scream  
  194. nokia-op-logo image/vnd.nok-oplogo-color  
  195. npx application/x-netfpx  
  196. nsnd audio/nsnd  
  197. nva application/x-neva1  
  198. oda application/oda  
  199. oom application/x-AtlasMate-Plugin  
  200. pac audio/x-pac  
  201. pae audio/x-epac  
  202. pan application/x-pan  
  203. pbm image/x-portable-bitmap  
  204. pcx image/x-pcx  
  205. pda image/x-pda  
  206. pdb chemical/x-pdb  
  207. pdf application/pdf  
  208. pfr application/font-tdpfr  
  209. pgm image/x-portable-graymap  
  210. pict image/x-pict  
  211. pm application/x-perl  
  212. pmd application/x-pmd  
  213. png image/png  
  214. pnm image/x-portable-anymap  
  215. pnz image/png  
  216. pot application/vnd.ms-powerpoint  
  217. ppm image/x-portable-pixmap  
  218. pps application/vnd.ms-powerpoint  
  219. ppt application/vnd.ms-powerpoint  
  220. pqf application/x-cprplayer  
  221. pqi application/cprplayer  
  222. prc application/x-prc  
  223. proxy application/x-ns-proxy-autoconfig  
  224. ps application/postscript  
  225. ptlk application/listenup  
  226. pub application/x-mspublisher  
  227. pvx video/x-pv-pvx  
  228. qcp audio/vnd.qcelp  
  229. qt video/quicktime  
  230. qti image/x-quicktime  
  231. qtif image/x-quicktime  
  232. r3t text/vnd.rn-realtext3d  
  233. ra audio/x-pn-realaudio  
  234. ram audio/x-pn-realaudio  
  235. rar application/x-rar-compressed  
  236. ras image/x-cmu-raster  
  237. rdf application/rdf+xml  
  238. rf image/vnd.rn-realflash  
  239. rgb image/x-rgb  
  240. rlf application/x-richlink  
  241. rm audio/x-pn-realaudio  
  242. rmf audio/x-rmf  
  243. rmm audio/x-pn-realaudio  
  244. rmvb audio/x-pn-realaudio  
  245. rnx application/vnd.rn-realplayer  
  246. roff application/x-troff  
  247. rp image/vnd.rn-realpix  
  248. rpm audio/x-pn-realaudio-plugin  
  249. rt text/vnd.rn-realtext  
  250. rte x-lml/x-gps  
  251. rtf application/rtf  
  252. rtg application/metastream  
  253. rtx text/richtext  
  254. rv video/vnd.rn-realvideo  
  255. rwc application/x-rogerwilco  
  256. s3m audio/x-mod  
  257. s3z audio/x-mod  
  258. sca application/x-supercard  
  259. scd application/x-msschedule  
  260. sdf application/e-score  
  261. sea application/x-stuffit  
  262. sgm text/x-sgml  
  263. sgml text/x-sgml  
  264. sh application/x-sh  
  265. shar application/x-shar  
  266. shtml magnus-internal/parsed-html  
  267. shw application/presentations  
  268. si6 image/si6  
  269. si7 image/vnd.stiwap.sis  
  270. si9 image/vnd.lgtwap.sis  
  271. sis application/vnd.symbian.install  
  272. sit application/x-stuffit  
  273. skd application/x-Koan  
  274. skm application/x-Koan  
  275. skp application/x-Koan  
  276. skt application/x-Koan  
  277. slc application/x-salsa  
  278. smd audio/x-smd  
  279. smi application/smil  
  280. smil application/smil  
  281. smp application/studiom  
  282. smz audio/x-smd  
  283. snd audio/basic  
  284. spc text/x-speech  
  285. spl application/futuresplash  
  286. spr application/x-sprite  
  287. sprite application/x-sprite  
  288. spt application/x-spt  
  289. src application/x-wais-source  
  290. stk application/hyperstudio  
  291. stm audio/x-mod  
  292. sv4cpio application/x-sv4cpio  
  293. sv4crc application/x-sv4crc  
  294. svf image/vnd  
  295. svg image/svg-xml  
  296. svh image/svh  
  297. svr x-world/x-svr  
  298. swf application/x-shockwave-flash  
  299. swfl application/x-shockwave-flash  
  300. t application/x-troff  
  301. tad application/octet-stream  
  302. talk text/x-speech  
  303. tar application/x-tar  
  304. taz application/x-tar  
  305. tbp application/x-timbuktu  
  306. tbt application/x-timbuktu  
  307. tcl application/x-tcl  
  308. tex application/x-tex  
  309. texi application/x-texinfo  
  310. texinfo application/x-texinfo  
  311. tgz application/x-tar  
  312. thm application/vnd.eri.thm  
  313. tif image/tiff  
  314. tiff image/tiff  
  315. tki application/x-tkined  
  316. tkined application/x-tkined  
  317. toc application/toc  
  318. toy image/toy  
  319. tr application/x-troff  
  320. trk x-lml/x-gps  
  321. trm application/x-msterminal  
  322. tsi audio/tsplayer  
  323. tsp application/dsptype  
  324. tsv text/tab-separated-values  
  325. tsv text/tab-separated-values  
  326. ttf application/octet-stream  
  327. ttz application/t-time  
  328. txt text/plain  
  329. ult audio/x-mod  
  330. ustar application/x-ustar  
  331. uu application/x-uuencode  
  332. uue application/x-uuencode  
  333. vcd application/x-cdlink  
  334. vcf text/x-vcard  
  335. vdo video/vdo  
  336. vib audio/vib  
  337. viv video/vivo  
  338. vivo video/vivo  
  339. vmd application/vocaltec-media-desc  
  340. vmf application/vocaltec-media-file  
  341. vmi application/x-dreamcast-vms-info  
  342. vms application/x-dreamcast-vms  
  343. vox audio/voxware  
  344. vqe audio/x-twinvq-plugin  
  345. vqf audio/x-twinvq  
  346. vql audio/x-twinvq  
  347. vre x-world/x-vream  
  348. vrml x-world/x-vrml  
  349. vrt x-world/x-vrt  
  350. vrw x-world/x-vream  
  351. vts workbook/formulaone  
  352. wav audio/x-wav  
  353. wax audio/x-ms-wax  
  354. wbmp image/vnd.wap.wbmp  
  355. web application/vnd.xara  
  356. wi image/wavelet  
  357. wis application/x-InstallShield  
  358. wm video/x-ms-wm  
  359. wma audio/x-ms-wma  
  360. wmd application/x-ms-wmd  
  361. wmf application/x-msmetafile  
  362. wml text/vnd.wap.wml  
  363. wmlc application/vnd.wap.wmlc  
  364. wmls text/vnd.wap.wmlscript  
  365. wmlsc application/vnd.wap.wmlscriptc  
  366. wmlscript text/vnd.wap.wmlscript  
  367. wmv audio/x-ms-wmv  
  368. wmx video/x-ms-wmx  
  369. wmz application/x-ms-wmz  
  370. wpng image/x-up-wpng  
  371. wpt x-lml/x-gps  
  372. wri application/x-mswrite  
  373. wrl x-world/x-vrml  
  374. wrz x-world/x-vrml  
  375. ws text/vnd.wap.wmlscript  
  376. wsc application/vnd.wap.wmlscriptc  
  377. wv video/wavelet  
  378. wvx video/x-ms-wvx  
  379. wxl application/x-wxl  
  380. x-gzip application/x-gzip  
  381. xar application/vnd.xara  
  382. xbm image/x-xbitmap  
  383. xdm application/x-xdma  
  384. xdma application/x-xdma  
  385. xdw application/vnd.fujixerox.docuworks  
  386. xht application/xhtml+xml  
  387. xhtm application/xhtml+xml  
  388. xhtml application/xhtml+xml  
  389. xla application/vnd.ms-excel  
  390. xlc application/vnd.ms-excel  
  391. xll application/x-excel  
  392. xlm application/vnd.ms-excel  
  393. xls application/vnd.ms-excel  
  394. xlt application/vnd.ms-excel  
  395. xlw application/vnd.ms-excel  
  396. xm audio/x-mod  
  397. xml text/xml  
  398. xmz audio/x-mod  
  399. xpi application/x-xpinstall  
  400. xpm image/x-xpixmap  
  401. xsit text/xml  
  402. xsl text/xml  
  403. xul text/xul  
  404. xwd image/x-xwindowdump  
  405. xyz chemical/x-pdb  
  406. yz1 application/x-yz1  
  407. z application/x-compress  
  408. zac application/x-zaurus-zac  
  409. zip application/zip </span>  

 關於office2007的幾個常用的mime類型爲

 

.dotx,application/vnd.openxmlformats-officedocument.wordprocessingml.template

.docx,application/vnd.openxmlformats-officedocument.wordprocessingml.document

.xlsx,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

.pptx,application/vnd.openxmlformats-officedocument.presentationml.presentation

 

到此,文件類型被限制住了,那麼當類型不對時,<result name="input">/index.jsp</result>這個就起作用了,意思

就是凡是上傳遇到的類型不是規定類型的文件時,都會跳到index.jsp就是還是上傳頁面並顯示錯誤信息,錯誤信息呢是利用了國際化配置在資源文件中的,頁面上用 <s:fielderror name="up"></s:fielderror>來顯示錯誤信息


假如我們限制類型爲: image/png,image/gif,image/jpeg,text/plain,application/pdf,text/html,application/msword


那麼上傳的效果爲

單獨上傳




 上傳後




 如果我們不點擊返回繼續上傳,而是點擊瀏覽器上的後退按鈕,那麼這時TOKEN將起作用,提示下面的頁面


如果我們上傳一個不在限定類型內的文件的話,也會報錯



 上傳後的結果



 對於批量上傳



 上傳後




發佈了7 篇原創文章 · 獲贊 6 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章