/**
* 获取网络文件大小 bytes
*
* @param url
* @return
* @throws IOException
*/
public static long getHttpFileContentLength(String url) throws IOException {
HttpURLConnection conn = null;
try {
URL urlObj = new URL(url);
conn = (HttpURLConnection) urlObj.openConnection();
conn.setRequestMethod("HEAD");
conn.setConnectTimeout(5000); // 连接超时 5 秒
conn.setReadTimeout(5000); // 读取超时 5 秒
return conn.getContentLengthLong(); // 获取文件大小
} finally {
if (conn != null) {
conn.disconnect(); // 关闭连接
}
}
}
/**
* 获取网络文件大小 bytes
*
* @param url
* @return
* @throws IOException
*/
public static long getHttpFileContentLength(String url) throws IOException {
HttpURLConnection conn = null;
try {
URL urlObj = new URL(url);
conn = (HttpURLConnection) urlObj.openConnection();
conn.setRequestMethod("HEAD");
conn.setConnectTimeout(5000); // 连接超时 5 秒
conn.setReadTimeout(5000); // 读取超时 5 秒
return conn.getContentLengthLong(); // 获取文件大小
} finally {
if (conn != null) {
conn.disconnect(); // 关闭连接
}
}
}