如何在 servlet 中识别使用 HTML 表单发送的请求是否具有 enctype multipart/form-data
还是默认的 application/x-www-form-urlencoded
?
或者:还有其他方法可以识别使用了哪种形式吗? request.getParameter("some_param")
仅适用于默认编码。
最佳答案
I'm using Apache Commons FileUpload for multipart, but wasn't sure how to switch between handling multipart and default forms
使用 Apache Commons FileUpload 自己的 ServletFileUpload#isMultipartContent()
检查一下。
if (ServletFileUpload.isMultipartContent(request)) {
// Parse with FileUpload.
}
else {
// Use normal getParameter().
}
另请参阅:
关于java - 识别 servlet 中的 html 表单或其编码(文本或文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10830693/