java servlet 能否确定发布的数据是否为多部分/表单数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/66423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:55:23  来源:igfitidea点击:

Can a servlet determine if the posted data is multipart/form-data?

javaservletsmultipartform-datafront-controller

提问by pkaeding

I have a servlet that is used for many different actions, used in the Front Controller pattern. Does anyone know if it is possible to tell if the data posted back to it is enctype="multipart/form-data"? I can't read the request parameters until I decide this, so I can't dispatch the request to the proper controller.

我有一个用于许多不同操作的 servlet,用于Front Controller 模式。有谁知道是否可以判断回传给它的数据是否为 ​​enctype="multipart/form-data"?在我做出决定之前,我无法读取请求参数,因此我无法将请求分派给正确的控制器。

Any ideas?

有任何想法吗?

回答by Darren Hicks

If you are going to try using the request.getContentType() method presented above, be aware that:

如果您打算尝试使用上面介绍的 request.getContentType() 方法,请注意:

  1. request.getContentType() may return null.
  2. request.getContentType() may not be equalto "multipart/form-data", but may just start with it.
  1. request.getContentType() 可能返回 null。
  2. request.getContentType() 可能不等于“multipart/form-data”,但可能只是从它开始。

With this in mind, the check you should run is :

考虑到这一点,您应该运行的检查是:

if (request.getContentType() != null && request.getContentType().toLowerCase().indexOf("multipart/form-data") > -1 ) {
// Multipart logic here
}

回答by Loren Segal

Yes, the Content-typeheader in the user agent's request should include multipart/form-dataas described in (at least) the HTML4 spec:

是的,Content-type用户代理请求中的标头应该包括multipart/form-data(至少)HTML4 规范中的描述:

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

回答by Kyle Boon

You can call a method to get the content type.

您可以调用一个方法来获取内容类型。

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html#getContentType()

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html#getContentType()

According to http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2, the content type will be "multipart/form-data".

根据http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2,内容类型将为“multipart/form-data”。

Don't forget that:

不要忘记:

  1. request.getContentType() may return null.

  2. request.getContentType() may not be equal to "multipart/form-data", but may just start with it.

  1. request.getContentType() 可能返回 null。

  2. request.getContentType() 可能不等于“multipart/form-data”,但可能只是从它开始。

So, with all this in mind:

因此,考虑到所有这些:

if (request.getContentType() != null && 
    request.getContentType().toLowerCase().indexOf("multipart/form-data") > -1 ) 
{
    << code block >>
} 

回答by awm129

ServletFileUpload implements isMultipartContent(). Perhaps you can lift this implementation (as opposed to going through the overhead to create a ServletFileUpload) for your needs.

ServletFileUpload 实现了 isMultipartContent()。也许您可以根据您的需要取消此实现(而不是通过开销来创建 ServletFileUpload)。

http://www.docjar.com/html/api/org/apache/commons/fileupload/servlet/ServletFileUpload.java.html

http://www.docjar.com/html/api/org/apache/commons/fileupload/servlet/ServletFileUpload.java.html

回答by enricopulatzo

You'll have to read the request parameters in order to determine this, at least on somelevel. The ServletRequest class has a getContentType method that you'll want to look at.

您必须阅读请求参数才能确定这一点,至少在某种程度上是这样。ServletRequest 类有一个您需要查看的 getContentType 方法。

回答by zb226

To expand on awm129's answer- Apache commons' implementation corresponds to this:

扩展awm129 的答案- Apache commons 的实现对应于:

if (request != null 
        && request.getContentType() != null 
        && request.getContentType().toLowerCase(Locale.ENGLISH).startsWith("multipart/")) {
    ...
}

You can write it a lot shorter using Apache commons' org.apache.commons.lang3.StringUtils:

您可以使用 Apache commons' 将其编写得更短org.apache.commons.lang3.StringUtils

if (StringUtils.startsWithIgnoreCase(request.getContentType(), "multipart/")) { 
    ... 
}

回答by SiuFay

https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getParts()

https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getParts()

java.util.Collection getParts()

java.util.Collection getParts()

Throws: ServletException - if this request is not of type multipart/form-data

抛出: ServletException - 如果这个请求不是 multipart/form-data 类型