2

如何在 servlet 中识别使用 HTML 表单发送的请求是 enctypemultipart/form-data还是 default application/x-www-form-urlencoded

或者:有没有其他方法可以识别使用了哪种形式?request.getParameter("some_param")仅适用于默认编码。

4

2 回答 2

1

Content-Type:您可以使用标头识别

if(HttpServletRequest.getContentType().contains("form-data")){
   //handle multipart data
 ....
} else if(HttpServletRequest.getContentType().contains("x-www-form-urlencoded")){
   //handle from data
 ....
}

如果 Web 容器支持Servlet 3.0,则使用 HttpServletRequest.getParts() API。

if(request.getParts() !=null){
  //handle multipart
} else {
  //handle form data
}
于 2012-05-31T09:52:19.250 回答
1

我正在将 Apache Commons FileUpload 用于多部分,但不确定如何在处理多部分和默认表单之间切换

使用 Apache Commons FileUpload 自己的ServletFileUpload#isMultipartContent()来检查它。

if (ServletFileUpload.isMultipartContent(request)) {
    // Parse with FileUpload.
}
else {
    // Use normal getParameter().
}

也可以看看:

于 2012-05-31T14:34:25.063 回答