简体   繁体   English

Java中文件下载的下载百分比

[英]Download percentage from file downloading in java

I got this code for downloading my files: 我获得了用于下载文件的以下代码:

public static void download()
            throws MalformedURLException, IOException
    {
        BufferedInputStream in = null;
        FileOutputStream out = null;
        try
        {
            in = new BufferedInputStream(new URL("https://www.dropbox.com/s/1uff8eeujplz4sf/files.zip?dl=1").openStream());
            out = new FileOutputStream(System.getProperty("user.home") + "/Adasti/files.zip");
            byte data[] = new byte[1024];
            int count;

            while ((count = in.read(data, 0, 1024)) != -1)
            {
                out.write(data, 0, count);
            }
        } catch (MalformedURLException e1)
        {
            e1.printStackTrace();
        } catch (IOException e2)
        {
            e2.printStackTrace();
        } finally
        {
            if (in != null)
                in.close();
            if (out != null)
                out.close();
        }
    }

I want to print out the percentage of the download while its downloading. 我想在下载时打印出下载百分比。 Is this possible with my method and if yes, how will I do it? 用我的方法可以做到吗,如果可以,我将如何做?

You have to get the file size before start the download. 开始下载之前,您必须获取文件大小。 Be carefull, not always the server can give you the file size (for sample streaming or some file server). 请注意,并非总是服务器可以为您提供文件大小(用于示例流或某些文件服务器)。 Try this: 尝试这个:

/**
 * 
 * @param remotePath
 * @param localPath
 */
public static void download(String remotePath, String localPath) {
    BufferedInputStream in = null;
    FileOutputStream out = null;

    try {
        URL url = new URL(remotePath);
        URLConnection conn = url.openConnection();
        int size = conn.getContentLength();

        if (size < 0) {
            System.out.println("Could not get the file size");
        } else {
            System.out.println("File size: " + size);
        }

        in = new BufferedInputStream(url.openStream());
        out = new FileOutputStream(localPath);
        byte data[] = new byte[1024];
        int count;
        double sumCount = 0.0;

        while ((count = in.read(data, 0, 1024)) != -1) {
            out.write(data, 0, count);

            sumCount += count;
            if (size > 0) {
                System.out.println("Percentace: " + (sumCount / size * 100.0) + "%");
            }
        }

    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (IOException e2) {
        e2.printStackTrace();
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (IOException e3) {
                e3.printStackTrace();
            }
        if (out != null)
            try {
                out.close();
            } catch (IOException e4) {
                e4.printStackTrace();
            }
    }
}

The call to this method is something like this: 对该方法的调用是这样的:

download("https://www.dropbox.com/s/1uff8eeujplz4sf/files.zip?dl=1", System.getProperty("user.home") + "/files.zip");

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

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