使用Java Apache Commons下载文件?

9 浏览
0 Comments

使用Java Apache Commons下载文件?

我该如何使用该库来下载文件并打印出已保存的字节数?我尝试使用以下代码,但无法显示字节或进度条。我应该使用哪种方法?

import static org.apache.commons.io.FileUtils.copyURLToFile;
public static void Download() {
    URL dl = null;
    File fl = null;
    try {
        fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip");
        dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip");
        copyURLToFile(dl, fl);
    } catch (Exception e) {
        System.out.println(e);
    }
}

public class download {
    public static void Download() {
        URL dl = null;
        File fl = null;
        String x = null;
        try {
            fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip");
            dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip");
            OutputStream os = new FileOutputStream(fl);
            InputStream is = dl.openStream();
            CountingOutputStream count = new CountingOutputStream(os);
            dl.openConnection().getHeaderField("Content-Length");
            IOUtils.copy(is, os);//begin transfer
            os.close();//close streams
            is.close();//^
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

0
0 Comments

问题的原因是想要下载文件时,希望可以获取文件的总字节数。可以通过http响应的Content-Length头字段来获取这个值。如果只想获取下载后的最终字节数,可以直接检查写入的文件大小。然而,如果想要显示当前下载的进度,可以通过扩展apache CountingOutputStream来实现,以便在每次调用write方法时计算通过的字节数并更新进度条。

为了解决这个问题,可以实现一个DownloadCountingOutputStream类,继承自CountingOutputStream,并重写afterWrite方法,在该方法中更新进度条。下面是一个简单的DownloadCountingOutputStream实现:

public class DownloadCountingOutputStream extends CountingOutputStream {
    private ActionListener listener = null;
    public DownloadCountingOutputStream(OutputStream out) {
        super(out);
    }
    public void setListener(ActionListener listener) {
        this.listener = listener;
    }
    protected void afterWrite(int n) throws IOException {
        super.afterWrite(n);
        if (listener != null) {
            listener.actionPerformed(new ActionEvent(this, 0, null));
        }
    }
}

使用示例:

public class Downloader {
    private static class ProgressListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Downloaded bytes : " + ((DownloadCountingOutputStream) e.getSource()).getByteCount());
        }
    }
    public static void main(String[] args) {
        URL dl = null;
        File fl = null;
        String x = null;
        OutputStream os = null;
        InputStream is = null;
        ProgressListener progressListener = new ProgressListener();
        try {
            fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip");
            dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip");
            os = new FileOutputStream(fl);
            is = dl.openStream();
            DownloadCountingOutputStream dcount = new DownloadCountingOutputStream(os);
            dcount.setListener(progressListener);
            dl.openConnection().getHeaderField("Content-Length");
            IOUtils.copy(is, dcount);
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            IOUtils.closeQuietly(os);
            IOUtils.closeQuietly(is);
        }
    }
}

以上是问题的解决方法和使用示例。

0
0 Comments

问题的出现原因:

这段内容讨论了如何使用Java Apache Commons库来下载文件。然而,有一个问题是如何获取下载文件的总字节数。流只能提供当前可用的字节数,但是由于流是连续的,还可能有更多的字节到来。为了解决这个问题,可以使用HTTP响应头中的Content-Length字段来获取总字节数。

解决方法:

要获取总字节数,需要进行以下步骤:

1. 调用url.openConnection()方法创建一个URLConnection对象。

2. 在URLConnection对象上调用getHeaderField("Content-Length")方法,该方法将返回一个字符串,其中包含总字节数。

3. 使用Integer.parseInt(bytesString)方法将字符串转换为整数,以获取总字节数。

代码示例:

URL url = new URL("http://example.com/file.pdf");
URLConnection connection = url.openConnection();
String contentLength = connection.getHeaderField("Content-Length");
int totalBytes = Integer.parseInt(contentLength);

通过上述代码,我们可以获取到文件的总字节数,从而可以进行进一步的处理,比如显示当前已下载的字节数。

0