java - 从java中下载文件的下载百分比

标签 java download

我得到了用于下载我的文件的代码:

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

我想在下载时打印出下载的百分比。我的方法是否可行?如果可行,我将如何实现?

最佳答案

您必须在开始下载之前获取文件大小。请注意,服务器并不总是可以为您提供文件大小(用于示例流或某些文件服务器)。试试这个:

/**
 * 
 * @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();
            }
    }
}

这个方法的调用是这样的:

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

关于java - 从java中下载文件的下载百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19054194/

相关文章:

java - Firebase 管理 SDK : NoSuchMethodError for setCredential()

Java文件下载休息服务,内容在byte[]数组中

java - 如何让 Eclipse 不下载 XSD?

macos - “此类型的文件可能会损害您的计算机的mac no keep选项”-是否没有“Keep”选项? macOS HS

android - 在 Android 中下载和提取 Zip 文件

java - LWUIT Gui 构建器

java - 不支持内容类型 'application/x-www-form-urlencoded;charset=UTF-8'

java - 对 Android 库使用提供的/compileOnly 依赖项

javascript - 从 JavaScript : Blob/createObjectURL vs. encodeURIComponent 中的字符串创建和下载文本文件

java - 为什么我们不应该在 Jboss 中使用自己的线程?