Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java中返回zip文件的RESTAPI?_Java_Api_Rest_Httpurlconnection_Bufferedreader - Fatal编程技术网

如何使用Java中返回zip文件的RESTAPI?

如何使用Java中返回zip文件的RESTAPI?,java,api,rest,httpurlconnection,bufferedreader,Java,Api,Rest,Httpurlconnection,Bufferedreader,我熟悉使用HttpURLConnection类在Java中发出GET请求的基础知识。在返回类型为JSON的正常情况下,我会这样做: URL obj = new URL(myURLString); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); int responseCode = con.getRespo

我熟悉使用HttpURLConnection类在Java中发出GET请求的基础知识。在返回类型为JSON的正常情况下,我会这样做:

    URL obj = new URL(myURLString);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    int responseCode = con.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK) {
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inpLine;
        StringBuffer resp = new StringBuffer();

        while ((inpLine = in.readLine()) != null) {
            resp.append(inputLine);
        }
        in.close();
        System.out.println(resp.toString());                          
    } else { System.out.println("Request failed");
但是,我尝试使用的当前端点会发回一个包含各种类型文件的zip文件。在这种情况下,“内容类型”将是“应用程序/八位字节流”。使用上述代码命中该端点会导致写入控制台的符号混乱(不确定其名称)。即使是在《邮递员》中,我也看到了同样的情况,只有当我在发出请求时使用“发送和下载”选项时,它才起作用,这会提示我保存响应并获得zip文件

关于如何点击API并通过Java下载返回的zip文件有什么帮助吗?先谢谢你


编辑:我打算用zip文件将其保存在本地,然后我的程序的其他部分将处理这些内容。

我想你可以试试这个API:

try (ZipInputStream zis = new ZipInputStream(con.getInputStream())) {
        
    ZipEntry entry; // kinda self-explained (a file inside zip)

    while ((entry = zis.getNextEntry()) != null) {
         // do whatever you need :)
         // this is just a dummy stuff
        System.out.format("File: %s Size: %d Last Modified %s %n",
                    entry.getName(), entry.getSize(),
                    LocalDate.ofEpochDay(entry.getTime() / MILLS_IN_DAY));
    }
}
在任何情况下,您都会得到一个流,因此您可以完成JavaIOAPI允许您完成的所有工作

例如,要保存文件,可以执行以下操作:

// I am skipping here exception handling, closing stream, etc.
byte[] zipContent = zis.readAllBytes();
new FileOutputStream("some.zip").write(zipContent);
 // again, skipping exceptions, closing, etc.
 // also, you'd probably do this in while loop as in the first example
 // so let's say we get ZipEntry 
 ZipEntry entry = zis.getNextEntry();
 
 // crate OutputStream to extract the entry from zip file
 final OutputStream os = new FileOutputStream("c:/someDir/" + entry.getName());
 
 
 byte[] buffer = new byte[1024];
 int length;
 
 //read the entry from zip file and extract it to disk
 while( (length = zis.read(buffer)) > 0) {
     os.write(buffer, 0, length);
 }

 // at this point you should get your file
要处理zip中的文件,您可以执行以下操作:

// I am skipping here exception handling, closing stream, etc.
byte[] zipContent = zis.readAllBytes();
new FileOutputStream("some.zip").write(zipContent);
 // again, skipping exceptions, closing, etc.
 // also, you'd probably do this in while loop as in the first example
 // so let's say we get ZipEntry 
 ZipEntry entry = zis.getNextEntry();
 
 // crate OutputStream to extract the entry from zip file
 final OutputStream os = new FileOutputStream("c:/someDir/" + entry.getName());
 
 
 byte[] buffer = new byte[1024];
 int length;
 
 //read the entry from zip file and extract it to disk
 while( (length = zis.read(buffer)) > 0) {
     os.write(buffer, 0, length);
 }

 // at this point you should get your file

我知道,这是一种低级API,您需要处理流、字节等,也许有一些LIB允许用一行代码或其他东西来实现这一点:)

zip文件是二进制文件。因此,您不能使用基于字符的读取器(您使用的是
InputStreamReader
)来读取内容。按原样接收输入流(因为它们是基于nbyte的)。您需要如何处理该zip文件?你需要把它保存在某个地方吗?或者你对某个内容感兴趣(在什么里面)?我需要把它保存在某个地方。端点上有一个参数,我可以指定保存它的位置。我不知道这是API,谢谢!但是,我仍在寻找一种方法来保存整个zip文件,或者理想情况下保存一个ZipEntry,这样我就可以遍历所有文件,并根据某些条件只保存我想要的文件。@Ars Updated:)非常感谢!唯一需要注意的是,根据jdk版本,ZipInputStream类可能不会继承readAllBytes()。尽管如此,我对保存整个拉链不感兴趣,所以这对我来说是可行的。非常感谢@ikos23