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
I assume your
buf
is a 2048 bytes array. Try the following changeReplace
with this
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.