我正在爲學校開展一個項目,我正在實施一個可用於從網上下載文件的工具(帶有限制選項)。事情是,我將擁有一個圖形用戶界面,我將使用一個JProgressBar
小部件,我希望顯示下載的當前進度。爲此我需要知道文件的大小。如何在下載文件之前獲取文件的大小。Java URLConnection:如何找出網頁文件的大小?
回答
任何HTTP響應是假定包含Content-Length標頭,因此您可以查詢URLConnection對象的該值。
//once the connection has been opened
List values = urlConnection.getHeaderFields().get("content-Length")
if (values != null && !values.isEmpty()) {
// getHeaderFields() returns a Map with key=(String) header
// name, value = List of String values for that header field.
// just use the first value here.
String sLength = (String) values.get(0);
if (sLength != null) {
//parse the length into an integer...
...
}
它可能並不總是能夠對服務器返回一個準確的內容長度,因此該值可能不準確,但至少你會得到一些可用價值的大部分時間。或者,現在我已經看到URLConnection javadoc更完全了,您可以使用getContentLength()方法。
您需要使用內容長度(URLConnection.getContentLength())。不幸的是,這並不總是準確的,也可能並不總是提供的,所以依靠它並不總是安全的。
如上所述,URLConnection的getContentLength是您最好的選擇,但它並不總是給出確定的長度。這是因爲HTTP協議(以及可能由URLConnection表示的其他協議)並不總是表示長度。
在HTTP的情況下,動態內容的長度通常不會預先知道—當通常發送content-length
標頭時。相反,另一個標頭transfer-coding
指定使用「分塊」編碼。使用分塊編碼時,整個響應的長度是未指定的,並且響應被分段發回,其中指定每個片段的大小。實際上,服務器緩衝servlet的輸出。每當緩衝區填滿時,就會發送另一個塊。使用這種機制,HTTP實際上可以開始流式傳輸無限長度的響應。
您可能無法通過URLConnection獲取文件長度的另一個原因是文件大於2 Gb(這是正整數的限制)。
+1的準確性。如果讀取URLConnection對象上的分塊響應的內容長度,J2SE 1.4將返回-1。 J2SE 1.3中不是這種情況。 – 2010-08-05 21:04:04
正如@erickson所說,有時候會有頭部「Transfer-Encoding:chunked」,而不是「Content-Length:」,當然你的長度爲空值。
關於available()方法 - 沒有人能保證它會返回適當的值,所以我建議你不要使用它。
使用HEAD請求,我讓我的web服務器回覆正確的內容長度字段,否則它是空的。我不知道,如果這個工程一般但對我來說它的作用:
private int tryGetFileSize(URL url) {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("HEAD");
conn.getInputStream();
return conn.getContentLength();
} catch (IOException e) {
return -1;
} finally {
conn.disconnect();
}
}
非常適合我,非常感謝。 – z00l 2012-01-31 19:50:39
//URLConnection connection
private int FileSize(String url) {
// this is the method and it get the url as a parameter.
// this java class will allow us to get the size of the file.
URLConnection con;
// its in a try and catch incase the url given is wrong or invalid
try{
// we open the stream
con = new URL(url).openConnection()
return con.getContentLength();
}catch (Exception e){
e.printStackTrace();
// this is returned if the connection went invalid or failed.
return 0;
}
}
- 1. 如何找出網頁的文件名?
- 2. html網頁生成的文件大小
- 3. 如何從網頁大小
- 4. 找出文件的大小C++
- 5. 如何找到文件的大小
- 6. 如何抓取特定大小的文件的網頁
- 7. 如何通過網絡傳輸大文件(文件大小>堆大小)?
- 8. 在Java中,如何找出我的最大堆大小?
- 9. 如何找出配置的最大java堆大小?
- 10. Java的.class文件大小Vs的java文件大小
- 11. 如何根據網頁瀏覽器控件大小設置網頁瀏覽器控件文檔體大小
- 12. java-how如何在創建之前找出txt文件的大小
- 13. PHP如何找出memcached陣列上的文件大小
- 14. 如何找到pdf文件大小?
- 15. Java:如何:有效地獲取2GB大小的文件大小?
- 16. 找出html頁面的大小
- 17. Java:找出對象的內存大小?
- 18. 找出抓取網頁時的圖像大小
- 19. Java最大文件大小
- 20. 如何找出nginx提供多少文件大小?
- 21. 如何找出JFrame的實際大小?
- 22. 如何找出sk_buff結構的大小?
- 23. 如何找出IE下的頁面下載時間/大小?
- 24. 如何找出操作系統中分頁的大小
- 25. 如何找出頁面大小是否已達到春季jpa
- 26. 如何比較本地文件與網上文件的大小?
- 27. 如何從網頁輸出svg文件?
- 28. 如何找出網頁的CPU瓶頸
- 29. 查找文件大小位
- 30. 找出CATextLayer中文本框的大小
getContentLength()鏈接給找不到網頁,看到http://docs.oracle.com/javase/6/ docs/api/java/net/URLConnection.html#getContentLength() – Reg 2014-03-17 05:04:31
getContentLength不會總是返回值。例如,執行一個請求時,會返回一個動態結果,其中內容的大小未知。 – coderama 2017-04-27 15:50:50