如何用java获取网络文件的大小

如题所述

try{
System.out.println("正在链接URL");
url=new URL("http://211.64.201.201/uploadfile/nyz.mp3");
HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();
//根据响应获取文件大小
fileLength=urlcon.getContentLength();
if(urlcon.getResponseCode()>=400){
System.out.println("服务器响应错误");
System.exit(-1);
}
if(fileLength<=0)
System.out.println("无法获知文件大小");
//打印信息
printMIME(urlcon);
System.out.println("文件大小为"+fileLength/1024+"K");
//获取文件名
String trueurl=urlcon.getURL().toString();
String filename=trueurl.substring(trueurl.lastIndexOf('/')+1);
fileOut=new File("D://",filename);
}
catch(MalformedURLException e){
System.err.println(e);
}
catch(IOException e){
System.err.println(e);
}
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网