Reputation: 1927
I have an issue that when I submit a valid Zip file from a Java program to my Web application, the Web Application interprets the Zip file as corrupted. Issuing the file directly to the Web Application via a Web Browser allows the same Zip file to be interpreted correctly. So what am I doing wrong in my Java program?
My Java Web Application can accept Zip files through a web service via HTTP POST. I looked at the bytes received on the Web Application and they are different if I send the file through the browser to when I send via my Java program.
I know that the Zip is correct since my Web Application accepts it without error when submitted through the Web Application. I just don't know what has gone wrong when I send it via my Java program. The error that occurs on the server side is :
java.util.zip.ZipException: invalid entry size (expected 231 but got 100 bytes)
I suspect the issue has to be with either how I am creating my payload (byte array) or the request properties (or lack of correct properties) on my URL connection. The code I am using to send my zip file is:
public static void main(String[] args) throws Exception {
// Convert the input file into a byte array
File inputFile = new File("C:/temp/myZip.zip");
FileInputStream fis = new FileInputStream(inputFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
}
Object payload = new String(bos.toByteArray());
fis.close();
// Setup a URL connection with the appropriate properties
URL url = new URL("http://localhost:8080/MyWebApp/ws/rest");
URLConnection urlc = url.openConnection();
urlc.setDoOutput(true);
urlc.setAllowUserInteraction(false);
urlc.addRequestProperty("Accept-Encoding", "gzip");
urlc.addRequestProperty("Content-Type", "application/text");
urlc.addRequestProperty("Content-Type", "application/octet-stream");
urlc.addRequestProperty("Content-Type", "multipart/form-data");
// Submit the payload to my Web Service
PrintStream ps = new PrintStream(urlc.getOutputStream());
ps.print(payload);
ps.close();
InputStream stream = getInputStream(urlc);
}
Any advice greatly appreciated.
Thanks,
Phil
Upvotes: 0
Views: 1733
Reputation: 336
Copy and execute this code to see what changes to some bytes can be:
// Convert the input file into a byte array byte[] theBytes = new byte[256]; // use to test all 256 byte values for(int i=0; i < theBytes.length; i++) theBytes[i] = (byte)i; ByteArrayInputStream bais = new ByteArrayInputStream(theBytes); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; for (int readNum; (readNum = bais.read(buf)) != -1;) { bos.write(buf, 0, readNum); //no doubt here is 0 }
Object payload = new String(bos.toByteArray());
// Submit the payload to my Web Service
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
ps.print(payload);
ps.close();
// Now test what was sent
byte[] endBytes = baos.toByteArray();
for(int i=0; i < endBytes.length; i++) {
if(endBytes[i] != (byte)i) {
System.out.println(i + " vs " + endBytes[i]); // show changed bytes
}
}
`
How can the above code be properly formatted???
Upvotes: 0
Reputation: 936
There is no issue with that how you create your byte array, you can test it by creating a new .zip
based on it
FileOutputStream fos = new FileOutputStream("C:/temp/myZip2.zip");
fos.write(bos.toByteArray());
Try to use .write()
with your byte array instead of using .print()
with a string. Accordingly to the javadoc both of them come down to .write()
but in your case you're sending byte representation of your string of byte representation of .zip
file instead of just byte representation of .zip
. I am not sure that it is a cause your problem but it worth a try.
PrintStream ps = new PrintStream(urlc.getOutputStream());
ps.write(bos.toByteArray());
ps.close();
If it didn't help - let me know how your server http://localhost:8080/MyWebApp/ws/rest
handles incomming file, what does it expect to receive.
Upvotes: 1