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

java中文件下载的下载百分比,java,download,Java,Download,我下载文件时得到了以下代码: public static void download() throws MalformedURLException, IOException { BufferedInputStream in = null; FileOutputStream out = null; try { in = new BufferedInputStream(new UR

我下载文件时得到了以下代码:

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

您必须在开始下载之前获得文件大小。请小心,服务器并不总是可以为样本流或某些文件服务器提供文件大小。试试这个:

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

计算每次迭代下载的字节数。展示出来,我该怎么做?我不知道如何诚实。您得到的HTTP响应应该包含一个内容长度头。查看如何检索它。这是您的总文件大小。然后,您应该保留一个总和,在每次迭代中添加count。然后显示比率。@Sotirios如果您提供示例代码,将不胜感激。计算每次迭代下载的字节数。展示出来,我该怎么做?我不知道如何诚实。您得到的HTTP响应应该包含一个内容长度头。查看如何检索它。这是您的总文件大小。然后,您应该保留一个总和,在每次迭代中添加count。然后显示比率。@Sotirios如果您提供示例代码,我们将不胜感激。谢谢您提及这一点。小心点,服务器不一定能提供文件大小。我得到了指示:谢谢你提到这件事。小心点,服务器不一定能提供文件大小。我知道方向了:谢谢