ZIP file corrupt fetching from SOAP attachment

倾然丶 夕夏残阳落幕 提交于 2019-12-11 11:24:34

问题


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

For ZIP files, the content type is binary.

I have this piece of code :

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;

}

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.

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.

Edit: 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.

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 changed to 3f , db changed to 3f , d6 changed to 3f , 85 changed to 3f , d0 changed to 3f ,

and so on.

Zip file contains a PDF and an XML file.

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


回答1:


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

Replace

fileStream.write(buf);

with this

fileStream.write(buf, 0, n);

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.



来源:https://stackoverflow.com/questions/15630096/zip-file-corrupt-fetching-from-soap-attachment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!