问题
A Servlet, running under Jetty 8, receives the following request:
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--
- Is there a convenient way of getting "data1here" and "data2here" from this kind of a request?
- Do Java servlets support it, natively?
- Or are there any additional libraries that support it?
回答1:
Consumes Annotation
Consume the event using a JAX-RS annotation provided by Apache CXF:
@Consumes("multipart/related")
From the JAX-RS documentation:
It is now possible (since 2.2.5) to have individual
multipart/form-dataparts read by registered JAX-RS MessageBodyReaders, something that is already possible to do for types likemultipart/mixedormultipart/related.
See also:
- Spring boot multipart/related mime type support
- Jersey: How to register MultiPartConfigProvider class
Note that Jersey, which is used by GlassFish, has no mention of related in its source code.
HTTP Client
Google offers an API for an HTTP client that parses multipart/related messages as per the RFC.
RESTeasy
The RESTeasy project can parse multipart/related content via JAX-RS.
JavaMail API
With some stream contortions, it may be possible to use the JavaMail API to parse a MimeMultipart message:
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.
The JavaMail FAQ offers a few more details:
As described above, there are more complex cases to consider as well. In particular, messages may have arbitrary nesting of
multipart/mixedandmultipart/alternativeparts and may includemultipart/relatedparts for embedded HTML andmultipart/signedand/ormultipart/encryptedparts for secure messages.
I'd advise against this approach because it mixes metaphors (it conflates MIME over HTTP/web with MIME over SMTP/mail). That is, JavaMail, conceptually, is used for reading and writing messages over SMTP/IMAP, which could leave future maintainers wondering why JavaMail is being used to parse MIME messages received via Servlets, especially when there are annotation-based solutions available. That said, documenting the code with the reason for its usage would be a way to avoid any confusion.
Container
Depending on the container, it may or may not handle all pertinent RFCs. You may have to try (or peruse the source code of) different containers to see what ones implement this feature.
Jetty
The source code has a few spots related to parsing input streams, which are limited to multipart/form-data:
- MultiPartFilter
- MultiPartInputStreamParser
Additionally, the unit tests do not include RFC 2387, which is a strong indicator that the container does not handle related parts under the Servlet 3.0 API. As such, JAX-RS is probably the best approach.
Tomcat
Tomcat has not implemented multipart/related as part of the Servlet 3.0 specification, although there exists a patched version of Tomcat 7.0.47 that does.
回答2:
You can use the JavaMail library and read the data in as "mail". You end up with a MimeMessage and can go through the BodyParts for processing. Usage is quite straight forward, I used this approach to implement an AS2 server and I've seen other implementations of AS2 doing the same, so it's a quite common approach.
I don't see a conflict of standards here. The whole request format is MIME, so handling it by a library intended for processing MIME encoded messages is in my eyes a correct way of handling things.
来源:https://stackoverflow.com/questions/46022763/handle-multipart-related-in-java-servlet