[英]Zip file becomes corrupted when sending from Java to WebService
我有一个问题,当我从Java程序向Web应用程序提交有效的Zip文件时,Web应用程序将Zip文件解释为已损坏。 通过Web浏览器直接将文件发布到Web应用程序可以正确解释相同的Zip文件。 那么我的Java程序在做什么错呢?
我的Java Web应用程序可以通过HTTP POST通过Web服务接受Zip文件。 我查看了Web应用程序上收到的字节,如果我通过浏览器发送文件到通过Java程序发送文件,它们是不同的。
我知道Zip是正确的,因为通过Web应用程序提交时,我的Web应用程序可以毫无错误地接受它。 当我通过Java程序发送错误消息时,我只是不知道出了什么问题。 在服务器端发生的错误是:
java.util.zip.ZipException:无效的条目大小(预期为231,但获得了100个字节)
我怀疑问题可能与我如何在URL连接上创建有效负载(字节数组)或请求属性(或缺少正确的属性)有关。 我用来发送zip文件的代码是:
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);
}
任何建议,不胜感激。
谢谢,
菲尔
创建字节数组没有任何问题,可以通过基于字节数组创建新的.zip
进行测试
FileOutputStream fos = new FileOutputStream("C:/temp/myZip2.zip");
fos.write(bos.toByteArray());
尝试对字节数组使用.write()
,而不是对字符串使用.print()
。 相应于javadoc,它们都归结为.write()
但是在您的情况下,您将发送.zip
文件的字节表示形式的字符串的字节表示,而不只是.zip
字节表示。 我不确定这是否是您造成问题的原因,但值得尝试。
PrintStream ps = new PrintStream(urlc.getOutputStream());
ps.write(bos.toByteArray());
ps.close();
如果没有帮助,请告诉我您的服务器http://localhost:8080/MyWebApp/ws/rest
处理传入的文件,它希望收到什么。
复制并执行此代码,以查看对某些字节所做的更改:
// 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
}
}
`
上面的代码如何正确格式化???
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.