Skip to content

改进了获取网络文件大小 #9

@Secd0g

Description

@Secd0g

/**
* 获取网络文件大小 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(); // 关闭连接
}
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions