Android获取网络文件大小

/**

* 获取网络文件大小

*/

private static long getFileLength(String downloadUrl) throws IOException{

if(downloadUrl == null || "".equals(downloadUrl)){

return 0L ;

}

URL url = new URL(downloadUrl);

HttpURLConnection conn = null;

try {

conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("HEAD");

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows 7; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36 YNoteCef/5.8.0.1 (Windows)");

return (long) conn.getContentLength();

} catch (IOException e) {

return 0L;

} finally {

conn.disconnect();

}

}
 版权声明:本文为AIDE教程网原创文章,转载请附上原文出处链接和本声明
本文链接: https://www.aidemx.cn/239.html

梦雪

专注于AIDE教程分享

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐