在 Jetty 8 下运行的 Servlet 收到以下请求:

Header: 
Content-Type = multipart/related; boundary=example 
 
Data: 
 
--example 
content-type: text/xml; charset=UTF-8 
 
data1here 
 
--example 
content-type: text/xml; charset=UTF-8 
 
data2here 
 
--example-- 
  • 有没有方便的方法从这种请求中获取“data1here”和“data2here”?
  • Java servlet 本身是否支持它?
  • 或者是否有任何其他库支持它?

请您参考如下方法:

消费注解

Consume使用 Apache CXF 提供的 JAX-RS 注释的事件:

@Consumes("multipart/related") 

来自 JAX-RS 文档:

It is now possible (since 2.2.5) to have individual multipart/form-data parts read by registered JAX-RS MessageBodyReaders, something that is already possible to do for types like multipart/mixed or multipart/related.

另见:

请注意,GlassFish 使用的 Jersey 在其 source code 中没有提及 related .

HTTP 客户端

Google 提供了一个 API对于 HTTP client根据 RFC 解析 multipart/related 消息.

RESTeasy

RESTeasy项目可以解析multipart/related通过 JAX-RS 的内容。

Java 邮件 API

通过一些流扭曲,可以使用 JavaMail API 来解析 MimeMultipart留言:

The default multipart subtype is "mixed". The other multipart subtypes, such as "alternative", "related", and so on, can be implemented as subclasses of MimeMultipart with additional methods to implement the additional semantics of that type of multipart content.

JavaMail FAQ提供更多细节:

As described above, there are more complex cases to consider as well. In particular, messages may have arbitrary nesting of multipart/mixed and multipart/alternative parts and may include multipart/related parts for embedded HTML and multipart/signed and/or multipart/encrypted parts for secure messages.

我建议不要使用这种方法,因为它混合了隐喻(它将 MIME over HTTP/web 与 MIME over SMTP/mail 混为一谈)。也就是说,从概念上讲,JavaMail 用于通过 SMTP/IMAP 读写消息,这可能会让 future 的维护者想知道为什么要使用 JavaMail 来解析通过 Servlet 接收的 MIME 消息,尤其是当有基于注释的解决方案可用时。也就是说,记录代码及其使用原因是避免混淆的一种方式。

容器

根据容器的不同,它可能处理也可能不处理所有相关的 RFC。您可能需要尝试(或仔细阅读)不同容器的源代码,以了解哪些容器实现了此功能。

jetty

source code有几个与解析输入流相关的地方,仅限于multipart/form-data:

另外,unit tests不包括 RFC 2387 ,这是容器不处理 Servlet 3.0 API 下的相关部分的有力指标。因此,JAX-RS 可能是最好的方法。

Tomcat

Tomcat 有 not implemented multipart/related 作为 Servlet 3.0 规范的一部分,尽管存在 Tomcat 7.0.47 的补丁版本。


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!