我試圖從使用Android的URL找到文件大小,但我得到getContentLength():-1,但如果我將在任何瀏覽器中打開url,那麼瀏覽器可以計算出文件大小,因爲瀏覽器是也是客戶端應用程序。Android getContentLength返回-1但在瀏覽器中返回文件大小,怎麼樣?
mycode的:
try {
URL url = new URL("https://www.dropbox.com/s/mk75lhvi96gkc00/match.flv?dl=0");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
// this will be useful to display download percentage
// might be -1: server did not report the length
int fileLength = connection.getContentLength();
//fileLength : -1;
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
這是我的Android上面的代碼,我得到-1作爲文件的長度,但是相同的URL文件的大小可以通過瀏覽器容易計算出來。
請給我一些解決方案。