我有 Java 代码,我尝试了很多变体,但没有帮助。
一些托管正在关闭文件内容长度(文件大小),因此请帮助我从 header 读取文件大小。
这是我的流代码的一部分,所以我需要打开列表并读取 int 值,对吗?
is = ucon.getInputStream();
fileLength = ucon.getContentLength();
List headersize = ucon.getHeaderFields().get("content-Lenght");
最佳答案
你可以这样做:
URL url = new URL("https://www.google.com/logos/classicplus.png");
URLConnection openConnection = url.openConnection();
System.out.println(openConnection.getContentLength());
根据documentation :
public int getContentLength()
Returns the value of the content-length header field.
Returns: the content length of the resource that this connection's URL references, or -1 if the content length is not known.
该方法执行的操作与您尝试手动执行的操作相同。如果未定义,您将收到 -1
关于java - 从 header 获取文件长度 Java Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11694978/