JAXB2 Mtom 附件被 BOM 损坏
我正在使用 JAXB2 在 Spring-WS 中执行 OXM。我指定的 XSD 需要将一个大型 XML 文件附加到肥皂消息,因此我使用 MTOM 来传输该文件,并在 JAXB2Marshaller 上启用了 MTOM。
当 JAXB2 编组具有预期 mime 类型 text/xml 的 MTOM 附件时,它会将该元素作为 javax.xml.transform.Source 对象提供。经过一番搜索后,我找到了如何将该 Source 对象发送到文件。
final Source source = request.getSource();
StreamSource streamSource = (StreamSource) source;
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
File file = new File ("/tempxmlfile.xml");
try{
transformer.transform(streamSource, new StreamResult(file));
LOG.info("File saved in "+file.getAbsolutePath());
}
catch(Exception ex){
ex.getMessage();
}
我遇到的问题是,当我将 UTF-8 编码的文件作为附件发送时,出现以下错误:
[Fatal Error] :1:1: Content is not allowed in prolog.
ERROR: 'Content is not allowed in prolog.'
这是由文件中编码文本前面的字节顺序标记引起的,尽管不需要此 BOM在 UTF-8 编码的文件中,这是 Unicode 标准允许的,Java 不支持 UTF-8 编码流中的 BOM。
我可以通过发送不带 BOM 的文件来解决此问题,但这实际上并不可行,因为这会导致大多数插入 BOM 的 Microsoft 产品出现问题。
对于 Sun/Oracle 拒绝解决流问题的解决方法有很多,但它们都要求您有权访问流,JAXB2 提供的源对象没有 InputStream,它只有一个 Reader 对象。我有没有办法解决这个问题,或者通过使用知道如何忽略 UTF-8 编码中的 BOM 的阅读器包装 Sources Reader 对象,或者更改 JAXB2 将附件读取到源中的方式,以便它可以忽略UTF-8 编码中的 BOM。
提前致谢, 克雷格
I'm using JAXB2 to to do OXM in a Spring-WS. The XSD I've specified requires a large XML file to be attached to the soap message so I'm using MTOM to transfer the file and have enabled MTOM on my JAXB2Marshaller.
When JAXB2 marshalls an MTOM attachment which has an expected mime type of text/xml it delivers that element as a javax.xml.transform.Source object. After some searching I was able to find out how I can send that Source object to a file.
final Source source = request.getSource();
StreamSource streamSource = (StreamSource) source;
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
File file = new File ("/tempxmlfile.xml");
try{
transformer.transform(streamSource, new StreamResult(file));
LOG.info("File saved in "+file.getAbsolutePath());
}
catch(Exception ex){
ex.getMessage();
}
The problem I am having is that when I send a UTF-8 encoded file as the attachment I get the following error:
[Fatal Error] :1:1: Content is not allowed in prolog.
ERROR: 'Content is not allowed in prolog.'
This is being caused by a Byte Order Mark in front of the encoded text in the file, although this BOM is not required in a UTF-8 encoded file it is allowed by the Unicode standard, Java does not support BOMs in UTF-8 encoded streams.
I can solve this problem by sending a file without the BOM but this is not really feasible as it will cause problems with most Microsoft products which do insert the BOM.
There are lots of workarounds for Sun/Oracle's refusal to fix this issue with the Streams but they all require you to have access to the Stream, the Source Object provided by JAXB2 does not have an InputStream it only has a Reader object. Is there a way for me to solve this problem, either by wrapping the Sources Reader object with a reader which knows how to ignore a BOM in UTF-8 encoding or to change the way JAXB2 reads the attachment into the source so that it can ignore the BOM in UTF-8 encoding.
Thanks in advance,
Craig
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
诀窍是“标记”读者。如果您的阅读器不支持标记,您可以将其包装在 BufferedReader 中:
选项 # 1 - 检查 BOM 并将其删除
我相信我的原始代码错误地写入了 BOM。下面的源代码更有意义:
选项#2 - 查找“<”并将读者推进到该点
阅读直到您点击“<”利用标记/重置:
The trick is to "mark" the Reader. If your reader does not support marking you can wrap it in a BufferedReader which does:
OPTION #1 - Check for BOM and Remove It
I believe my original code was writing the BOM incorrectly. The source code below makes more sense:
OPTION #2 - Find '<' and Advance Reader to that Point
Read until you hit the '<' leveraging mark/reset: