如何将 javax.xml.transform.Source 转换为 InputStream?

发布于 2024-09-19 11:04:07 字数 234 浏览 4 评论 0原文

如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2024-09-26 11:04:08

首先尝试向下转换为 javax.xml.transform.stream.StreamSource。如果成功,您就可以通过 getter 访问底层的 InputStreamReader。这将是最简单的方法。

如果向下转换失败,您可以尝试使用 javax.xml.transform.Transformer 将其转换为已使用java.io.ByteArrayOutputStream。然后返回一个java.io.ByteArrayInputStream。类似于:

Transformer t = // getTransformer();
ByteArrayOutputStream os = new ByteArrayOutputStream();
Result result = new StreamResult(os);
t.transform(inputSource, result);
return new ByteArrayInputStream(os.getByteArray());

当然,如果StreamSource可以是一个大文档,那么这是不可取的。在这种情况下,您可以使用临时文件和 java.io.FileOutputStream/java.io.FileInputStram。另一种选择是生成一个转换器线程并通过 java.io.PipedOutputStream/java.io.PipedInputStream 进行通信,但这更复杂:

PipedInputStream is = new PipedInputStream();
PipedOutputStream os = new PipedOutputStream(is);
Result result = new StreamResult(os);
// This creates and starts a thread that creates a transformer
// and applies it to the method parameters.
spawnTransformerThread(inputSource, result);
return is;

First try to downcast to javax.xml.transform.stream.StreamSource. If that succeeds you have access to the underlying InputStream or Reader through getters. This would be the easiest way.

If downcasting fails, you can try using a javax.xml.transform.Transformer to transform it into a javax.xml.transform.stream.StreamResult that has been setup with a java.io.ByteArrayOutputStream. Then you return a java.io.ByteArrayInputStream. Something like:

Transformer t = // getTransformer();
ByteArrayOutputStream os = new ByteArrayOutputStream();
Result result = new StreamResult(os);
t.transform(inputSource, result);
return new ByteArrayInputStream(os.getByteArray());

Of course, if the StreamSource can be a large document, this is not advisable. In that case, you could use a temporary file and java.io.FileOutputStream/java.io.FileInputStram. Another option would be to spawn a transformer thread and communicate through java.io.PipedOutputStream/java.io.PipedInputStream, but this is more complex:

PipedInputStream is = new PipedInputStream();
PipedOutputStream os = new PipedOutputStream(is);
Result result = new StreamResult(os);
// This creates and starts a thread that creates a transformer
// and applies it to the method parameters.
spawnTransformerThread(inputSource, result);
return is;
不及他 2024-09-26 11:04:08

通常不可能,除非它可以向下转换为 StreamSource 或其他

It's not normally possible, unless it can be down-casted to StreamSource or other implementations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文