문제

How do I recognize in a servlet whether the request sent using HTML form has enctype multipart/form-data or the default application/x-www-form-urlencoded?

Alternatively: is there any other way to recognize which form was used? request.getParameter("some_param") works only with default encoding.

도움이 되었습니까?

해결책

I'm using Apache Commons FileUpload for multipart, but wasn't sure how to switch between handling multipart and default forms

Use Apache Commons FileUpload's own ServletFileUpload#isMultipartContent() to check it.

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

See also:

다른 팁

You can identify using Content-Type: header

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

If the web container supports Servlet 3.0, the use HttpServletRequest.getParts() API.

if(request.getParts() !=null){
  //handle multipart
} else {
  //handle form data
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top