servlet:将 ServletInputStream 转换为 FileInputStream

发布于 2024-09-14 01:21:08 字数 453 浏览 4 评论 0原文

我正在编写一个 servlet,它接收 xml 文件,将其提供给另一个类,并将带有一些注释的 html 文件返回给客户端。 我得到的 input-xml 类似于:

input = request.getInputStream();

但此输入是 ServletInputStream,另一个类(用于注释)需要 FileInputStream。

如果我向 XMLEventReader(在另一个类中)提供 ServletInputStream,我会收到解析错误:

ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.

我认为这是因为 servletinputstream 而不是 fileinputstream。

问候 我希望有人能够帮助我:)

i'm writing a servlet that receives a xml file, gives it to another class and gives a html file with some comments back to the client.
I'm getting the input-xml with something like:

input = request.getInputStream();

but this input is a ServletInputStream and the other class(for the comments) needs a FileInputStream.

If i give the XMLEventReader(in the other class) the ServletInputStream, i get a parsing error:

ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.

And i think this is because of the servletinputstream instead of the fileinputstream.

Greetings
i hope somebody is able to help me:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

凉栀 2024-09-21 01:21:08

我认为后一个问题不太可能是由于它是 ServletInputStream 所致。解析器不应该关心数据的来源。

我认为输入流中的数据更有可能不正确。

如果可能的话,当前需要 FileInputStream 的类应该重构为与 InputStream 一起使用...否则您将不得不编写数据首先到一个实际的文件,这显然不理想。

I think it's unlikely that the latter problem is due to it being a ServletInputStream. The parser shouldn't care about the source of the data.

I think it's rather more likely that the data in the input stream is incorrect.

Your class which currently requires FileInputStream should be refactored to work with InputStream if at all possible... otherwise you'll have to write the data to an actual file first, which is obviously not ideal.

眼眸里的那抹悲凉 2024-09-21 01:21:08

您应该从 ServletInputStream 读取数据,并将其转储到 FileOutputStream 中。这样您就可以查看正在发送的数据,然后使用 FileInputStream 以及您描述的需要它的类单独测试该数据。我的猜测是,同样的事情也会发生,因为就像之前的海报所建议的那样,数据可能格式错误。

you should read the data from the ServletInputStream, and dump it into a FileOutputStream. this way you can look at the data that is being sent, then test that data separately using FileInputStream with the class you described that needs it. my guess is that the same thing will happen as is happening now since like the previous poster suggested, the data is probably in the wrong format.

枯叶蝶 2024-09-21 01:21:08

好吧,我现在聪明了一点:)
问题是:在 ServletInputStream 的开头(最后一行)有一些标头信息(如内容类型等)。
有没有聪明的解决方案来剪切这些信息?

问候

Okay, i am now a bit smarter:)
The problem is: In the ServletInputStream is at the beginning(and one line at the end) some header information (as content-type, etc..).
Is there a smart solution for cutting this information?

greetings

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