Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getContentLength()是否下载整个文件?_Java_Image_Download - Fatal编程技术网

Java getContentLength()是否下载整个文件?

Java getContentLength()是否下载整个文件?,java,image,download,Java,Image,Download,这是我下载图像的代码宽度百分比[Java]: URL url = new URL(imageUrl); OutputStream os; URLConnection connection = url.openConnection(); connection.connect(); int fileLenth = connection.getContentLength(); try (InputStream is = url.openStream()) {

这是我下载图像的代码宽度百分比[Java]:

URL url = new URL(imageUrl);
    OutputStream os;
    URLConnection connection = url.openConnection();
    connection.connect();
    int fileLenth = connection.getContentLength();
    try (InputStream is = url.openStream()) {
        os = new FileOutputStream(destinationFile);
        byte[] b = new byte[2048];
        int length;
        int count = 0;
        double sumCount = 0.0;
        while ((length = is.read(b)) != -1) {
            os.write(b, 0, length);

            sumCount += count;
            if (fileLenth > 0) {
                System.out.println("Percentace: " + (sumCount / fileLenth * 100.0) + "%");
            }
        }
    }
    os.close();
我想知道我何时使用:

URLConnection connection = url.openConnection();
connection.connect();
int fileLenth = connection.getContentLength();
文件是否已下载? 如果这是真的,文件将下载两次,这是不好的 你能帮我取得最好的成绩吗?
谢谢这个
getContentLength()
方法没有下载这个文件,它只返回
content length
头的值(如果有),

@Titus你应该回答这个问题。@Kayaman谢谢你的建议,我刚刚就这么做了。