从SOAP附件提取ZIP文件损坏 [英] ZIP file corrupt fetching from SOAP attachment

查看:93
本文介绍了从SOAP附件提取ZIP文件损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Web服务,该服务可以帮助我从消息中心获取文件. 文件类型可以是XML或ZIP.

I am using a web service that helps me to fetch files from a message hub. File type can either be XML or ZIP.

对于ZIP文件,内容类型为二进制.

For ZIP files, the content type is binary.

我有这段代码:

private String extractPayload(String filePath, AttachmentPart attach){

        FileOutputStream fileStream = null;

        try {
              DataHandler handler;
              handler = attach.getDataHandler();

              File f = new File(filePath);

              fileStream = new FileOutputStream(filePath);

              handler.writeTo(fileStream);

              fileStream.flush();

        } catch (Exception ex) {

              logger.info("####Exception:" + ex.getMessage());

        } finally {

              if (fileStream != null)
                    fileStream.close();

        }

        return filePath;

}

现在,该代码可以很好地用于提取XML文件,尽管对于ZIP文件,该文件实际上已损坏. 我从Messaging Hub使用文件实用工具下载了相同的文件,发现通过SOAP附件获取的文件大小比实际大小大4字节左右.

Now, the code works fine for fetching XML files, although, in case of ZIP files, the file turns out to be corrupt. I downloaded the same file using file utility from the Messaging Hub and found out that the Size of File that i fetch through SOAP Attachment is around 4 bytes more then its actual size.

更新:附件编码为7位(如果可能会阻塞),而另一个包含另一个zip的附件则编码为Quote-printable.这两个zip都是从相同的Web服务获取的(尽管它们的编码方式不同),而且都被破坏了.

Update : The attachment encoding is 7-Bit (if that might be obstructing) while another attachment containing another zip is encoded Quote-printable. Both the zips are being fetched from same web service (although they differ in encoding), and both turn out corrupt.

我强烈认为问题出在我正在接收文件的编码中,这是实际文件与接收到的附件文件之间的比较.

I strongly feel that the problem is with the encoding in which i am receiving file and here is a comparison between the actual file and the received attachment file.

实际文件大小:9031字节 收到的附件文件大小:9066字节

Actual File size : 9031 bytes Received Attachment File size : 9066 bytes

我尝试在文档编辑器中比较两个文件,以找出两者之间的差异. 原始文件与提取的附件文件之间的差异(二进制编辑器):

I tried comparing both files in a document editor to find the differences between both. Original file to fetched attachment file differences (Binary editor):

ed更改为3f, db更改为3f, d6更改为3f, 85更改为3f, d0更改为3f,

ed changed to 3f , db changed to 3f , d6 changed to 3f , 85 changed to 3f , d0 changed to 3f ,

以此类推.

Zip文件包含PDF和XML文件.

Zip file contains a PDF and an XML file.

两个文件的起始行都相似,以PK开头<​​/p>

The start line of both files are similar, starting with PK

推荐答案

我假设您的buf是一个2048字节的数组.尝试以下更改

I assume your buf is a 2048 bytes array. Try the following change

替换

fileStream.write(buf);

与此

fileStream.write(buf, 0, n);

这是要修复的情况,以防您在上一次读取时仅得到1024个字节,然后buf的其他1024个字节将具有垃圾值并将被写入文件并损坏它.

This is to fix in case in your last read you get only 1024 bytes then the other 1024 bytes of the buf will have garbage values and will get written to file and will corrupt it.

这篇关于从SOAP附件提取ZIP文件损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆