用 Java 处理下载

发布于 2024-08-28 13:46:22 字数 123 浏览 4 评论 0原文

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

How would I be able to handle downloads using HttpResponse in Java? I made an HttpGet request to a specific site - the site returns the file to be downloaded. How can I handle this download? InputStream doesn't seem to be able to handle it (or maybe I'm using it the wrong way.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风月客 2024-09-04 13:46:22

假设您实际上正在谈论 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) {}
        }
    }

}

在这里工作正常。你的问题出在别的地方。

Assuming you're actually talking about HttpClient, Here's an 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) {}
        }
    }

}

Works fine here. Your problem lies somewhere else.

得不到的就毁灭 2024-09-04 13:46:22

打开流并发送文件:

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" );
}

Open a stream and send the file:

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" );
}
仙女 2024-09-04 13:46:22

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

即,

response.setContentType(.. relevant content type)

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

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

In general when you want the browser to show the download dialog box for a file to be downloaded, you should set the incoming inputstream content directly into the response object steam and set the content type of response (HttpServletResponse object) to the relevant file type.

i.e.,

response.setContentType(.. relevant content type)

Content type can be application/pdf for pdf files as an example.

If browser has a plugin to show relevant file in the browser window, file will open and user can save then, otherwise browser will show the download box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文