简体   繁体   English

从Java发送到WebService时Zip文件损坏

[英]Zip file becomes corrupted when sending from Java to WebService

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. 我有一个问题,当我从Java程序向Web应用程序提交有效的Zip文件时,Web应用程序将Zip文件解释为已损坏。 Issuing the file directly to the Web Application via a Web Browser allows the same Zip file to be interpreted correctly. 通过Web浏览器直接将文件发布到Web应用程序可以正确解释相同的Zip文件。 So what am I doing wrong in my Java program? 那么我的Java程序在做什么错呢?

My Java Web Application can accept Zip files through a web service via HTTP POST. 我的Java Web应用程序可以通过HTTP POST通过Web服务接受Zip文件。 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. 我查看了Web应用程序上收到的字节,如果我通过浏览器发送文件到通过Java程序发送文件,它们是不同的。

I know that the Zip is correct since my Web Application accepts it without error when submitted through the Web Application. 我知道Zip是正确的,因为通过Web应用程序提交时,我的Web应用程序可以毫无错误地接受它。 I just don't know what has gone wrong when I send it via my Java program. 当我通过Java程序发送错误消息时,我只是不知道出了什么问题。 The error that occurs on the server side is : 在服务器端发生的错误是:

java.util.zip.ZipException: invalid entry size (expected 231 but got 100 bytes) java.util.zip.ZipException:无效的条目大小(预期为231,但获得了100个字节)

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. 我怀疑问题可能与我如何在URL连接上创建有效负载(字节数组)或请求属性(或缺少正确的属性)有关。 The code I am using to send my zip file is: 我用来发送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);
}

Any advice greatly appreciated. 任何建议,不胜感激。

Thanks, 谢谢,

Phil 菲尔

There is no issue with that how you create your byte array, you can test it by creating a new .zip based on it 创建字节数组没有任何问题,可以通过基于字节数组创建新的.zip进行测试

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. 尝试对字节数组使用.write() ,而不是对字符串使用.print() 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 . 相应于javadoc,它们都归结为.write()但是在您的情况下,您将发送.zip文件的字节表示形式的字符串的字节表示,而不只是.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. 如果没有帮助,请告诉我您的服务器http://localhost:8080/MyWebApp/ws/rest处理传入的文件,它希望收到什么。

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??? 上面的代码如何正确格式化???

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM