通常可以通过检查请求是否为multipart来检测。
以下示例代码是来自Apache Commons FileUpload 库的 c&p
/**
* 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/";