2013-02-14 25 views
0

我想檢測一個Http-Request是否是文件上傳。我知道,有一種觀點可能表明文件上傳的東西:如何檢測文件上傳檢查http消息?

  • 請求方法:如果它的POST,通常有一個消息體。但我知道,也可以通過GET請求發送內容。是否可以使用GET請求上傳文件?
  • content-type:我猜想,content-type字段通常是設置file-upload-message。但是什麼是文件上傳的內容類型?
  • content-length:應該爲文件上傳設置content-length字段。

有左視圖問題:

我怎麼能區分一個普通的HTML格式的後一個文件上傳?瀏覽器是否使用分塊編碼進行文件上傳? (據我所知,這將是毫無意義的,但我不知道很多)

回答

1

通常可以通過檢查請求是否多部分檢測到。

以下示例代碼爲c從Apache的通用FileUpload庫& P(http://javasourcecode.org/html/open-source/commons-fileupload/commons-fileupload-1.2.2/org/apache/commons/fileupload/servlet/ServletFileUpload.java.html

/** 
* Utility method that determines whether the request contains multipart 
* content. 
* 
* @param request The servlet request to be evaluated. Must be non-null. 
* 
* @return <code>true</code> if the request is multipart; 
*   <code>false</code> otherwise. 
*/ 
public static final boolean isMultipartContent(
     HttpServletRequest request) { 
    if (!"post".equals(request.getMethod().toLowerCase())) { 
     return false; 
    } 
    String contentType = request.getContentType(); 
    if (contentType == null) { 
     return false; 
    } 
    if (contentType.toLowerCase().startsWith(MULTIPART)) { 
     return true; 
    } 
    return false; 
} 

其中MULTIPART是

/** 
* Part of HTTP content type header. 
*/ 
public static final String MULTIPART = "multipart/";