Skip to content

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

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(); // 关闭连接
}
}
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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