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

來源:互聯網
上載者:User
文章目錄
  • 總結

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

<param name="allowedTypes">image/png,image/bmp,image/jpg</param>

但是,用過這個參數的人都知道,allowedTypes是“檔案類型”, 而不是“檔案尾碼名”,檔案類型與檔案尾碼名有什麼區別呢?

就如尾碼名為bmp的圖片的檔案類型為image/bmp,尾碼名為xls的Excel檔案類型為application/vnd.ms-excel等等....

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

猜想是否可以根據尾碼名來過濾允許上次的檔案,Struts如此紅火的架構應該能想到這點。

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

protected Set<String> allowedTypesSet = Collections.emptySet();protected Set<String> allowedExtensionsSet = Collections.emptySet();

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

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

所以,我們可以大膽的猜想,allowedExtensions參數就是用於配置”允許上傳的檔案尾碼名“。

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

protected boolean acceptFile(Object action, File file, String filename, String contentType, String inputName, ValidationAware validation, Locale locale) {    boolean fileIsAcceptable = false;    // If it's null the upload failed    if (file == null) {        String errMsg = getTextMessage(action, "struts.messages.error.uploading", new Object[]{inputName}, locale);        if (validation != null) {            validation.addFieldError(inputName, errMsg);        }        LOG.warn(errMsg);    } else if (maximumSize != null && maximumSize < file.length()) {        String errMsg = getTextMessage(action, "struts.messages.error.file.too.large", new Object[]{inputName, filename, file.getName(), "" + file.length()}, locale);        if (validation != null) {            validation.addFieldError(inputName, errMsg);        }        LOG.warn(errMsg);    } else if ((!allowedTypesSet.isEmpty()) && (!containsItem(allowedTypesSet, contentType))) {        String errMsg = getTextMessage(action, "struts.messages.error.content.type.not.allowed", new Object[]{inputName, filename, file.getName(), contentType}, locale);        if (validation != null) {            validation.addFieldError(inputName, errMsg);        }        LOG.warn(errMsg);    } else if ((!allowedExtensionsSet.isEmpty()) && (!hasAllowedExtension(allowedExtensionsSet, filename))) {        String errMsg = getTextMessage(action, "struts.messages.error.file.extension.not.allowed", new Object[]{inputName, filename, file.getName(), contentType}, locale);        if (validation != null) {            validation.addFieldError(inputName, errMsg);        }        LOG.warn(errMsg);    } else {        fileIsAcceptable = true;    }    return fileIsAcceptable;}

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

} else if (allowedTypesSet不為空白 && allowedTypesSet不包含該檔案的類型) {// 添加錯誤資訊....} else if (allowedExtensionsSet不為空白 && allowedExtensionsSet不包含該檔案的尾碼名) {// 添加錯誤資訊}

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

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

總結

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

<!-- 允許尾碼名為png,bmp,jpg,doc,xls的檔案上傳 --><param name="allowedExtensions">png,bmp,jpg,doc,xls</param>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.