Java getContentLength()是否下载整个文件?
这是我下载图像的代码宽度百分比[Java]: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()) {
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谢谢你的建议,我刚刚就这么做了。