servlet:将 ServletInputStream 转换为 FileInputStream
我正在编写一个 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。
问候 我希望有人能够帮助我:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为后一个问题不太可能是由于它是 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 withInputStream
if at all possible... otherwise you'll have to write the data to an actual file first, which is obviously not ideal.您应该从 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.
好吧,我现在聪明了一点:)
问题是:在 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