gpt4 book ai didi

java - 从 header Java Android 获取文件长度

转载 作者:可可西里 更新时间:2023-11-01 16:34:32 24 4
gpt4 key购买 nike

我有 Java 代码,我尝试了很多变体但没有帮助。
一些主机正在关闭文件内容长度(文件大小)所以请帮我从标题中读取文件大小。
这是我的流代码的一部分,所以我需要打开列表并读取 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com