如何使用 Java 下载远程文件

How to download a remote file using Java

我尝试使用尽可能少的第三方库从 Web 服务器(http 或 https)下载单个文件。

我想出的方法如下:

private static final int BUFFER_SIZE = 8;

public static boolean download(URL url, File f) throws IOException {
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    FileOutputStream out = new FileOutputStream(f);
    BufferedInputStream in = new BufferedInputStream(conn.getInputStream());

    byte[] buffer;
    long dld = 0, expected = conn.getContentLengthLong(); // TODO expected will be -1 if the content length is unknown
    while (true) { // TODO fix endless loop if server timeout
        buffer = new byte[BUFFER_SIZE];
        int n = in.read(buffer);
        if (n == -1) break;
        else dld += n;
        out.write(buffer);
    }
    out.close();
    System.out.println(dld + "B transmitted to " + f.getAbsolutePath());
    return true;
}

但是,它绝不会按预期工作。例如我尝试下载https://upload.wikimedia.org/wikipedia/commons/6/6d/Rubber_Duck_Florentijn_Hofman_Hong_Kong_2013d.jpg,结果很恐怖:

出于某种原因,我可以在 IrfanView 中查看图片,但在任何其他查看器中都无法查看,所以这是重新保存的版本。

我尝试调整缓冲区大小或下载其他图像,但结果大致相同。

如果我查看该文件,其中的全部内容都被简单地替换为点:

我真的迷失在这个问题上,所以感谢您的帮助:)

当没有8个字节的数据可供读取时,就会出现问题。这使得数组的一部分充满了零,这就是为什么您在十六进制编辑器中看到这么多。

解决方法很简单:用out.write(buffer, 0, n);替换out.write(buffer);。这告诉 FileOutputStream 只读取索引 0n 之间的字节。

固定代码:

private static final int BUFFER_SIZE = 8;

public static boolean download(URL url, File f) throws IOException {
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    FileOutputStream out = new FileOutputStream(f);
    BufferedInputStream in = new BufferedInputStream(conn.getInputStream());

    // We can move the buffer declaration outside the loop
    byte[] buffer = new byte[BUFFER_SIZE];

    long dld = 0, expected = conn.getContentLengthLong(); // TODO expected will be -1 if the content length is unknown
    while (true) {
        int n = in.read(buffer);
        if (n == -1) break;
        else dld += n;
        out.write(buffer, 0, n);
    }
    out.close();
    System.out.println(dld + "B transmitted to " + f.getAbsolutePath());
    return true;
}

试试这样下载图片

public static byte[] download(String param) throws IOException {
    InputStream in = null;
    ByteArrayOutputStream out = null;

    try {
        URL url = new URL(param);
        HttpURLConnection con = (HttpURLConnection)url.openConnection(); 
        con.setConnectTimeout(120000);
        con.setReadTimeout(120000);
        con.setRequestMethod("GET");
        con.connect();
        in = new BufferedInputStream(con.getInputStream());
        out = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int n = 0;
        while (-1 != (n = in.read(buf))) {
            out.write(buf, 0, n);
        }

        return out.toByteArray();

    } finally {
        try {
            out.close();
        } catch (Exception e1) {

        }
        try {
            in.close();
        } catch (Exception e2) {

        }
    }
}