【问题标题】:Handling downloads in Java在 Java 中处理下载
【发布时间】:2010-04-13 20:34:29
【问题描述】:

如何在 Java 中使用 HttpResponse 处理下载?我向特定站点发出了 HttpGet 请求 - 该站点返回要下载的文件。我该如何处理这个下载? InputStream 似乎无法处理它(或者我可能使用错误的方式。)

【问题讨论】:

  • 你在说什么 API/库? Apache HttpComponents HttpClient v4?如果您不知道,请提及您正在谈论的HttpResponseHttpGet 类的包名称,最好发布SSCCE,以便我们发现您的错误。
  • 确实我正在使用 Apache HttpComponents。您发布的答案似乎是我正在寻找的。但是,是否可以将所有输入存储到字符串而不是实际文件?我将输入流转换为字符串方法使用缓冲阅读器,但它给了我 null。

标签: java apache-httpcomponents


【解决方案1】:

假设您实际上是在谈论HttpClient,这里是SSCCE

package com.stackoverflow.q2633002;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class Test {

    public static void main(String... args) throws IOException {
        System.out.println("Connecting...");
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet("http://apache.cyberuse.com/httpcomponents/httpclient/binary/httpcomponents-client-4.0.1-bin.zip");
        HttpResponse response = client.execute(get);

        InputStream input = null;
        OutputStream output = null;
        byte[] buffer = new byte[1024];

        try {
            System.out.println("Downloading file...");
            input = response.getEntity().getContent();
            output = new FileOutputStream("/tmp/httpcomponents-client-4.0.1-bin.zip");
            for (int length; (length = input.read(buffer)) > 0;) {
                output.write(buffer, 0, length);
            }
            System.out.println("File successfully downloaded!");
        } finally {
            if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
            if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
        }
    }

}

在这里工作正常。您的问题出在其他地方。

【讨论】:

  • 我将内容类型添加到标题(Application/octet-stream)并使用相同的方法,这似乎可以解决问题。
【解决方案2】:

打开一个流并发送文件:

try {
    FileInputStream is = new FileInputStream( _backupDirectory + filename );
    OutputStream os = response.getOutputStream();
    byte[] buffer = new byte[65536];
    int numRead;
    while ( ( numRead = is.read( buffer, 0, buffer.length ) ) != -1 ) {
        os.write( buffer, 0, numRead );
    }
    os.close();
    is.close();
}
    catch (FileNotFoundException fnfe) {
    System.out.println( "File " + filename + " not found" );
}

【讨论】:

    【解决方案3】:

    一般来说,当你希望浏览器显示要下载文件的下载对话框时,你应该将传入的inputstream内容直接设置到响应对象steam中,并设置响应的内容类型(HttpServletResponse对象) 到相关的文件类型。

    即,

    response.setContentType(.. relevant content type)
    

    以pdf文件为例,内容类型可以是application/pdf

    如果浏览器有插件可以在浏览器窗口显示相关文件,文件会打开并保存,否则浏览器会显示下载框。

    【讨论】:

    • 猜对了,但他说的不是 Servlet API :)
    • 嗯...我想我是因为提到了 httpresponse 而得意忘形.. 抱歉:)
    猜你喜欢
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    相关资源
    最近更新 更多