如何将 javax.xml.transform.Source 转换为 InputStream?
如何将 javax.xml.transform.Source
转换为 InputStream? Source
的实现是javax.xml.transform.dom.DOMSource
。
Source inputSource = messageContext.getRequest().getPayloadSource();
How can I convert a javax.xml.transform.Source
into a InputStream? The Implementation of Source
is javax.xml.transform.dom.DOMSource
.
Source inputSource = messageContext.getRequest().getPayloadSource();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先尝试向下转换为
javax.xml.transform.stream.StreamSource
。如果成功,您就可以通过 getter 访问底层的InputStream
或Reader
。这将是最简单的方法。如果向下转换失败,您可以尝试使用
javax.xml.transform.Transformer
将其转换为已使用java.io.ByteArrayOutputStream
。然后返回一个java.io.ByteArrayInputStream
。类似于:当然,如果
StreamSource
可以是一个大文档,那么这是不可取的。在这种情况下,您可以使用临时文件和 java.io.FileOutputStream/java.io.FileInputStram。另一种选择是生成一个转换器线程并通过 java.io.PipedOutputStream/java.io.PipedInputStream 进行通信,但这更复杂:First try to downcast to
javax.xml.transform.stream.StreamSource
. If that succeeds you have access to the underlyingInputStream
orReader
through getters. This would be the easiest way.If downcasting fails, you can try using a
javax.xml.transform.Transformer
to transform it into ajavax.xml.transform.stream.StreamResult
that has been setup with ajava.io.ByteArrayOutputStream
. Then you return ajava.io.ByteArrayInputStream
. Something like:Of course, if the
StreamSource
can be a large document, this is not advisable. In that case, you could use a temporary file andjava.io.FileOutputStream
/java.io.FileInputStram
. Another option would be to spawn a transformer thread and communicate throughjava.io.PipedOutputStream
/java.io.PipedInputStream
, but this is more complex:通常不可能,除非它可以向下转换为 StreamSource 或其他
It's not normally possible, unless it can be down-casted to StreamSource or other implementations.