gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:57:13 25 4
gpt4 key购买 nike

这是我的下载图像宽度百分比代码[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 header (如果有的话)的值,more details here

关于java - getContentLength() 是否下载整个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33330168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com