繁体   English   中英

使用 java11 HttpClient.sendAsync() 下载 zip 文件

[英]Download zip file using java11 HttpClient.sendAsync()

我正在尝试使用 GitHub ZDB974238714CAA8DE634A7CE1DZ6Z 程序通过 java 程序下载 zip 文件。

我正在使用的程序如下:

public static void main(String[] args) {
        // create client
        HttpClient client = HttpClient.newHttpClient();

        // create request
        HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://api.github.com/repos/:owner/:repo/zipball/:ref")).build();

        // use the client to send the asynchronous request
        InputStream is = client.sendAsync(request, BodyHandlers.ofInputStream())
                .thenApply(HttpResponse::body).join();
        try {
            FileOutputStream out = new FileOutputStream("outputZipFile.zip");
            copy(is,out,1024);
            out.close();
        }catch(Exception e) {}
        
        
        
    }

    private static void copy(InputStream is, FileOutputStream out, int i) {
        // TODO Auto-generated method stub
        byte[] buf = new byte[i];
        try {
            int n = is.read(buf);
            while(n>=0) {
                out.write(buf,0,n);
                n=is.read(buf);
            }
            out.flush();
        }catch(IOException ioe) {
            System.out.println(ioe.getStackTrace());
        }
        
    }

当我尝试运行它时,我得到了空的主体,因此 output 文件也将为空。 我注意到使用 Java11 HttpClient 的 HttpURLConnection 可以使它工作,但我更喜欢使用这个 Java11 功能来发送异步请求。

我不明白我做错了什么。

编辑:我目前使用的 HttpURLConnection 代码如下:

private void downloadVersion(String sha, String outputDestination) {
            try {
                URL url = new URL( getDownloadQuery(sha) );
                
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                if(authToken!=null) 
                    connection.setRequestProperty("Authorization", "Bearer " + authToken) ;
                connection.setRequestMethod("GET");
                
                InputStream in = connection.getInputStream();
    
                FileOutputStream out = new FileOutputStream(outputDestination);
                copy(in, out, 1024);
                out.close();
            } catch (Exception e) {}
            
        }

Your url (when set to correct github repos) may be returning redirect status 302. To make HTTP client follow redirects replace HttpClient client = HttpClient.newHttpClient() with use of HttpClient.newBuilder() . 您还可以通过尝试资源和使用InputStream.transferTo来简化代码:

HttpClient client = HttpClient.newBuilder().followRedirects(Redirect.ALWAYS).build();

URI uri = URI.create("https://api.github.com/repos/:owner/:repo/zipball/:ref");
HttpRequest request = HttpRequest.newBuilder().uri(uri).build();

// use the client to send the asynchronous request
InputStream is = client.sendAsync(request, BodyHandlers.ofInputStream())
        .thenApply(HttpResponse::body).join();
try (FileOutputStream out = new FileOutputStream("outputZipFile.zip")) {
    is.transferTo(out);
}

暂无
暂无

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

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