我是Java新手。我试图从URL获取文件大小但是我失败了可以帮助我如何获取以字节为单位的URL大小吗?
答案 0 :(得分:2)
File file =new File("xxxxx"); // file path
if(file.exists()){
double bytes = file.length();
double kilobytes = (bytes / 1024);
System.out.println("bytes : " + bytes);
double megabytes = (kilobytes / 1024);
System.out.println("kilobytes : " + kilobytes);
double gigabytes = (megabytes / 1024);
System.out.println("megabytes : " + megabytes);
double terabytes = (gigabytes / 1024);
System.out.println("gigabytes : " + gigabytes);
double petabytes = (terabytes / 1024);
}else{
System.out.println("File does not exists!");
}
答案 1 :(得分:2)
答案 2 :(得分:0)
public int getFileSizeInBytes(String path){
try {
URL url=new URL(path);
URLConnection urlConnection=url.openConnection();
urlConnection.connect();
int file_size=urlConnection.getContentLength();
return file_size;
} catch (MalformedURLException e) {
}catch (IOException e){
}
return -1;
}