HttpServletRequest 输入流损坏?

发布于 2024-11-16 04:18:07 字数 649 浏览 6 评论 0原文

我通过 java servlet 接收 SOAPMessage,其附件部分包含多部分 mime。执行此操作:

 InputStream inputStream = request.getInputStream();           
 byte[] data = IOUtils.toByteArray(inputStream);

 File file = new File("/usr/local/user/message.txt");
 FileOutputStream fos = new FileOutputStream(file);
 fos.write(data);
 fos.close();

为我提供多部分 mime 中二进制代码的损坏数据。任何人都知道为什么会发生这种情况?我将二进制数据复制到文件中,并将文件扩展名更改为适当的图像扩展名,但我得到的图像已损坏。比较message.txt和.snoop文件的十六进制数据表明,十六进制数据存在细微差别,导致失真。 ASCII 数据没有变化。谢谢。

存在失真,正如您可以看到图片一开始看起来是正确的,但过了一会儿它开始看起来很奇怪,因为十六进制值是错误的。例如:执行上述操作后,客户端的 06 04 04 04 04 04 06 04 04 06 0a 变为 06 04 04 04 04 04 06 04 04 06 0D 。

I'm receiving a SOAPMessage over a java servlet with an attachmentpart containing a multipart mime. Doing this:

 InputStream inputStream = request.getInputStream();           
 byte[] data = IOUtils.toByteArray(inputStream);

 File file = new File("/usr/local/user/message.txt");
 FileOutputStream fos = new FileOutputStream(file);
 fos.write(data);
 fos.close();

Gives me corrupted data for the binary code in the multipart mime. Anyone knows why this is happening? I copied the binary data to a file and changed the file extension to the appropriate image extension but I'm getting a corrupted image. Comparing hexadecimal data of message.txt and a .snoop file shows that there are slight differences in the hexadecimal data causing the distortion. There were no changes in the ASCII data. Thanks.

There is distortion, as in you can see the picture looks correct at the start, but it starts looking weird after a while because the hexadecimal values are wrong. For example: 06 04 04 04 04 04 06 04 04 06 0a from the client becomes 06 04 04 04 04 04 06 04 04 06 0D after I do the above.

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

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

发布评论

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

评论(2

月野兔 2024-11-23 04:18:07

根据评论,该文件似乎已正确写入磁盘(这是我的解释)。

WinSCP 用于将文件从 (Linux/Unix) 计算机传输到 Windows 计算机。传输过程中采用的传输模式可能会导致 WinSCP 将所有 UNIX 风格的行结尾转换为 Windows 风格的行结尾,或者反之亦然,具体取决于传输的来源。当采用文本模式传输文件时,或者当允许 WinSCP 确定是否应使用文本模式或二进制模式传输文件时,会发生这种情况。您可能已将其设置为默认模式,因此强制以二进制方式传输文件;如果这不起作用,您需要进一步调查。

如果 WinSCP 似乎是此问题的原因,您可以通过查看两个平台上文件的二进制内容来确认。您可以使用 Linux/Unix 上的 xxd。在 Windows 上,此问题中列出的实用程序之一将帮助。

您还可以使用任何其他文件复制协议,如 FTP、SFTP,甚至使用 NFS 或 Samba 来传输文件,并确定 WinSCP 是原因。

Based on the comments, it appears that the file is being written to disk correctly (that is my interpretation).

WinSCP is used to transfer the file from the (Linux/Unix) machine to a Windows machine. The mode of transfer employed in the transfer process could result in WinSCP converting all unix-style line endings to Windows-style line endings, or vice-versa depending on where the transfer originated from. This occurs when text mode is employed to transfer the file, or when WinSCP is allowed to determine whether it should use the text or binary mode to transfer the file. You might have set it to the default mode, so force the file to be transferred in binary; if this hasn't worked, you'll need to investigate further.

If WinSCP appears to be the cause of this problem, you could confirm it, by viewing the binary contents of the files on both platforms. You could use xxd on Linux/Unix. On Windows, one of the utilities listed in this question will help.

You may also use any other file copy protocols like FTP, SFTP or even use NFS or Samba to transfer the files, and establish WinSCP to be the cause.

阳光下的泡沫是彩色的 2024-11-23 04:18:07

对于多部分 MIME,您可以使用 Apache Fileupload 库。你可以在这里找到它:

Apache Fileupload 下载路径

入门教程

For multipart MIME you can use Apache Fileupload library. you can find it here :

Apache Fileupload download path

Starter tutorial

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