使用java rest客户端获取zip文件(restEasy) [英] Getting a zip file with a java rest client (restEasy)

查看:356
本文介绍了使用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();
  }

这篇关于使用java rest客户端获取zip文件(restEasy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆