Struts文件上傳allowedTypes問題,煩人的“允許上傳的文件類型”

Struts文件上傳allowedTypes問題,煩人的“允許上傳的文件類型”


Struts的文件上傳問題,相信很多人都會使用allowedTypes參數來配置允許上傳的文件類型,如下。

[html] view plain copy
  1. <param name="allowedTypes">  
  2.     image/png,image/bmp,image/jpg  
  3. </param>  

但是,用過這個參數的人都知道,allowedTypes是“文件類型”, 而不是“文件後綴名”,文件類型與文件後綴名有什麼區別呢?

就如後綴名爲bmp的圖片的文件類型爲image/bmp,後綴名爲xls的Excel文件類型爲application/vnd.ms-excel等等....

這各種各樣的”文件類型“,讓人煩不勝煩。。。。

猜想是否可以根據後綴名來過濾允許上次的文件,Struts如此紅火的框架應該能想到這點。

於是便打開Struts文件上次的攔截器org.apache.struts2.interceptor.FileUploadInterceptor一看,發現如下代碼:

[java] view plain copy
  1. protected Set<String> allowedTypesSet = Collections.emptySet();  
  2. protected Set<String> allowedExtensionsSet = Collections.emptySet();  

看到一個allowedTypesSet和一個allowedExtensionsSet,很容易想到,前者是用於存放參數allowedTypes的,

而後者呢,自然是用於存放參數allowedExtensions的,extension翻爲:延長、擴展...

所以,我們可以大膽的猜想,allowedExtensions參數就是用於配置”允許上傳的文件後綴名“

再來看看FileUploadInterceptor裏的一個方法acceptFile(),此方法用於根據當前配置,檢查該文件是否允許被上傳

[java] view plain copy
  1. protected boolean acceptFile(Object action, File file, String filename, String contentType, String inputName, ValidationAware validation, Locale locale) {  
  2.     boolean fileIsAcceptable = false;  
  3.   
  4.     // If it's null the upload failed  
  5.     if (file == null) {  
  6.         String errMsg = getTextMessage(action, "struts.messages.error.uploading"new Object[]{inputName}, locale);  
  7.         if (validation != null) {  
  8.             validation.addFieldError(inputName, errMsg);  
  9.         }  
  10.   
  11.         LOG.warn(errMsg);  
  12.     } else if (maximumSize != null && maximumSize < file.length()) {  
  13.         String errMsg = getTextMessage(action, "struts.messages.error.file.too.large"new Object[]{inputName, filename, file.getName(), "" + file.length()}, locale);  
  14.         if (validation != null) {  
  15.             validation.addFieldError(inputName, errMsg);  
  16.         }  
  17.   
  18.         LOG.warn(errMsg);  
  19.     } else if ((!allowedTypesSet.isEmpty()) && (!containsItem(allowedTypesSet, contentType))) {  
  20.         String errMsg = getTextMessage(action, "struts.messages.error.content.type.not.allowed"new Object[]{inputName, filename, file.getName(), contentType}, locale);  
  21.         if (validation != null) {  
  22.             validation.addFieldError(inputName, errMsg);  
  23.         }  
  24.   
  25.         LOG.warn(errMsg);  
  26.     } else if ((!allowedExtensionsSet.isEmpty()) && (!hasAllowedExtension(allowedExtensionsSet, filename))) {  
  27.         String errMsg = getTextMessage(action, "struts.messages.error.file.extension.not.allowed"new Object[]{inputName, filename, file.getName(), contentType}, locale);  
  28.         if (validation != null) {  
  29.             validation.addFieldError(inputName, errMsg);  
  30.         }  
  31.   
  32.         LOG.warn(errMsg);  
  33.     } else {  
  34.         fileIsAcceptable = true;  
  35.     }  
  36.   
  37.     return fileIsAcceptable;  
  38. }  

別的先不管,先看看到第2,3個else if節點,分別是利用了allowedTypesSet和allowedExtensionsSet,如下

[java] view plain copy
  1. else if (allowedTypesSet不爲空 && allowedTypesSet不包含該文件的類型) {  
  2.   
  3. // 添加錯誤信息....  
  4.   
  5. else if (allowedExtensionsSet不爲空 && allowedExtensionsSet不包含該文件的後綴名) {  
  6.   
  7. // 添加錯誤信息  
  8.   
  9. }  

從上面的代碼中可以看出,如果我們要利用allowedExtensions參數來控制上傳文件的後綴名,則不能配置allowedTypes參數。

否則,如果allowedTypes參數有配置,那麼allowedExtensions參數將不會再起效。

總結

使用Struts文件上次功能,我們可以使用”文件類型“和”文件後綴名“兩者中的一個來控制上傳文件的類型/後綴名。但是,allowedTypes的優先級別高於allowedExtensions,如果配置了allowedTypes則allowedExtensions將不再起效。最後附上allowedExtensions的一個簡單配置:

[html] view plain copy
  1. <!-- 允許後綴名爲png,bmp,jpg,doc,xls的文件上傳 -->     
  2. <param name="allowedExtensions">  
  3.     png,bmp,jpg,doc,xls  
  4. </param>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章