简体   繁体   English

Java下载文件并获取当前下载完成

[英]Java downloading a file and retrieving current download completion

In a program I have that auto-updates, I need it to download a specific file (working already) and it does so with the following code: 在一个具有我会自动更新的程序中,我需要它来下载特定文件(已经工作),并且使用以下代码来执行此操作:

public static void getFile() {
    try {
        URL url = new URL("https://dl.dropboxusercontent.com/s/tc301v61zt0v5cd/texture_pack.png?dl=1");
        InputStream in = new BufferedInputStream(url.openStream());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int n = 0;
        while (-1 != (n = in.read(buf))) {
            out.write(buf, 0, n);
        }
        out.close();
        in.close();
        byte[] response = out.toByteArray();
        FileOutputStream fos = new FileOutputStream(file4);
        fos.write(response);
        fos.close();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Check your internet connection, then try again.\nOr re-install the program.\nError Message 7", "Could Not Download The Required Resources.", JOptionPane.NO_OPTION);
        e.printStackTrace();
        System.exit(0);
    }
}

How would I implement a way to get the current completion of the download (like the percent it's downloaded) and put it into an integer to be printed out in the console (just for developer testing). 我将如何实现一种方式来获取下载的当前完成情况(例如已下载的百分比)并将其放入整数以在控制台中打印出来(仅用于开发人员测试)。 Also what sort of equation could i use to get the estimated amount of time left on the download? 另外,我可以使用哪种方程式来获得估计的剩余下载时间? Anything would help! 会有什么帮助! Thanks. 谢谢。

If you examine the HTTP headers sent in the file download, you'll discover the file size. 如果检查文件下载中发送的HTTP标头,则会发现文件大小。 From this you can calculate percentage complete: 由此可以计算完成百分比:

curl --head "https://dl.dropboxusercontent.com/s/tc301v61zt0v5cd/texture_pack.png?dl=1"

Gives you this: 给你这个:

HTTP/1.1 200 OK
accept-ranges: bytes
cache-control: max-age=0
content-disposition: attachment; filename="texture_pack.png"
Content-length: 29187
Content-Type: image/png
Date: Mon, 28 Apr 2014 22:38:34 GMT
etag: 121936d
pragma: public
Server: nginx
x-dropbox-request-id: 1948ddaaa2df2bdf2c4a2ce3fdbeb349
X-RequestId: 4d9ce90907637e06728713be03e6815d
x-server-response-time: 514
Connection: keep-alive

You may have to use something more advanced than standard Java library classes for your download to access the headers however, something like Apache HttpClient. 您可能需要使用比标准Java库类更高级的下载内容来访问标头,例如Apache HttpClient。

There is no way to get the size of a streamed file without streaming to the end of the file, so it can't be done within that block of code. 没有流到文件末尾就无法获得流文件的大小,因此无法在该代码块内完成。

If you are willing to adopt the Dropbox API, you can use that to get the size of the file before starting the download. 如果您愿意采用Dropbox API,则可以在开始下载之前使用它来获取文件的大小。 Then you can work with that size and the number bytes downloaded ( n in your code) to achieve what you need. 然后,您可以使用该大小和下载的字节数(代码中的n )来实现所需的功能。

The class in the Dropbox API that you need is DbxEntry.File . Dropbox API中所需的类是DbxEntry.File

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

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