使用java rest客户端获取zip文件(restEasy)
每个人。
我开始使用restEasy(jboss)java rest客户端,遇到了一个我似乎无法解决的问题。
到目前为止,我可以使用它从休息服务器(字符串形式)带回json。
然而,我需要的其他服务之一带回一个zip文件。我偶然发现了一个问题。
以下是代码:
I started to use the restEasy (jboss) java rest client and met a problem I cannot seem to solve. So far, I could use it to bring back json from the rest server (string form). One of the rest service I need brings back a zip file, however. And I stumbled on a problem. Here is the code :
ClientRequest req = new ClientRequest("rest service url"); //the url is good
ClientResponse<String> res = null;
res = req.get(String.class);
ZipInputStream zip = new ZipInputStream(new
ByteArrayInputStream(res.getEntity().getBytes()));
ZipEntry zipEntry = zip.getNextEntry();
System.out.println(zipEntry.getName());
//here, I print the name of the first file in my archive, so, I seem to have a
// zip file indeed
String jsonString = IOUtils.toString(zip);
//bam, this is causing a zipException : invalid block type
谷歌告诉我这是读取zip文件的正确方法。我也尝试逐字节读取它,它会在zip.read()上抛出sams异常。
Google told me that it was the correct way to read a zip file. I tried to read it byte by byte, too, and It throws the sams exception on zip.read().
我做错了什么?
如何阅读文件内容?
Did I do something wrong? What Should I do to read the content of my file?
我很感激有关此事的任何见解。
谢谢
I would be gratefull for any insight on that matter. Thanks
PS:对不起,如果我听起来很奇怪,英语不是我的第一语言。
P.S : Sorry if I sound strange, English is not my first language.
URL url = new URL("http://xyz.com/download.zip");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
IntpuStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream("download.zip");
copy(in, out, 1024);
out.close();
public static void copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
byte[] buf = new byte[bufferSize];
int n = input.read(buf);
while (n >= 0) {
output.write(buf, 0, n);
n = input.read(buf);
}
output.flush();
}