限制Java HttpUrlConnection获取内容长度 [英] Limitation in Java HttpUrlConnection getting content length

查看:2607
本文介绍了限制Java HttpUrlConnection获取内容长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对了,我开始了我的项目,创建一个很好的Java下载管理器。我目前看到的唯一问题是当我要求URL的内容长度。当我使用HttpUrlConnection的getContentLength提供的方法时,它返回一个Int。



如果我只能下载文件大小不超过2GB的文件,这是非常好的。做更多的挖掘,发现java不支持unsigned int值,但即使这样只会给我一个最大为4GB的文件大小。



任何人都可以帮助我正确地获取大于2GB的文件的内容大小。



提前感谢



更新



感谢您的回答Elite Gentleman ,我尝试了你建议的,但是不断返回一个空值,所以我做的是

  HttpURLConnection conn =(HttpURLConnection) absoluteUrl.openConnection(); 
conn.connect();
long contentLength = Long.parseLong(conn.getHeaderField(Content-Length));

由于某些原因,使用conn.getRequestProperty总是为我返回null

解决方案

为什么不尝试使用headerField方法获取内容长度?即

  HttpURLConnection connection = new HttpURLConnection(...); 
long contentLength = Long.parseLong(connection.getHeaderField(Content-Length));

它相当于 connection.getContentLength ,除了返回值为string类型。这可以给出位长度的准确结果。



更新:我更新了它以使用HeaderField而不是requestProperty ...感谢作者这个问题。






感谢来自Ru的@FlasH,您可以使用JDK 7及更高版本实现相同的结果。 / p>



这实际上是调用新的 URLConnection.getHeaderFieldLong() 方法,这也在JDK 7及更高版本中引入。


Right so, I started my project on creating a Java download manager which is going nicely. The only problem I am seeing at the moment is when i request the content length of the URL. When i use the method provided by HttpUrlConnection's getContentLength, it returns an Int.

This is great if i was only to ever download files that were less than 2GB in file size. Doing some more digging and find that java does not support unsigned int values, but even so that would only give me a file size of a 4GB max.

Can anyone help me in the right direction of getting the content size of a file that is greater than 2GB.

Thanks in advance.

UPDATE

Thanks for you answer Elite Gentleman, i tried what you suggested but that kept returning a null value, so instead what i did was

HttpURLConnection conn = (HttpURLConnection) absoluteUrl.openConnection();
conn.connect();
long contentLength = Long.parseLong(conn.getHeaderField("Content-Length"));

For some reason just using conn.getRequestProperty always returned a null for me

解决方案

Why not try use the headerField method for getting content length? i.e.

HttpURLConnection connection = new HttpURLConnection(...); 
long contentLength = Long.parseLong(connection.getHeaderField("Content-Length"));

It's equivalent as connection.getContentLength except that the return value is of type string. This can give accurate result of bit length.

UPDATE: I updated it to use HeaderField instead of requestProperty...thanks to the author of the question.


Thanks to @FlasH from Ru, you can achieve the same result using from JDK 7 and higher.

This, in effect calls the new URLConnection.getHeaderFieldLong() method, which has also been introduced in JDK 7 and higher.

这篇关于限制Java HttpUrlConnection获取内容长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆