Limit the length of content obtained by Java httpurlconnection
Yes, so I started to create a java download manager project that worked well The only problem I see so far is when I ask for the content length of the URL When I use the method provided by getcontentlength of httpurlconnection, it returns an int
It's great if I just want to download files with a file size of no more than 2GB After more mining, I found that Java does not support unsigned int value, but even this will only give me a maximum file size of 4GB
Anyone can help me get the content size of files larger than 2GB correctly
Thank you in advance
UPDATE
Thank you for answering elite gentleman. I tried what you suggested, but always returned a null value, so I did, not
HttpURLConnection conn = (HttpURLConnection) absoluteUrl.openConnection(); conn.connect(); long contentLength = Long.parseLong(conn.getHeaderField("Content-Length"));
For some reason, just using conn.getrequestproperty always returns a null for me
Solution
Why not try to get the content length using the headerfield method? Namely
HttpURLConnection connection = new HttpURLConnection(...); long contentLength = Long.parseLong(connection.getHeaderField("Content-Length"));
It is equivalent to connection Getcontentlength, except that the return value is of type string This gives an accurate result of bit length
Update: I updated it to use headerfield instead of requestproperty... Thanks to the author of the question
Thanks to @ flame from Ru, you can use JDK 7 and later to achieve the same results
> URLConnection. getContentLengthLong().
This is actually called the new urlconnection Getheaderfieldlong () method, which is also introduced in JDK 7 and later