HttpUrlConnection.getContentLength()返回-1但标题字段存在

时间:2014-11-13 08:40:44

标签: java php httpurlconnection

我遇到了Java应用程序从Web服务器下载文件的奇怪行为。我下载APK文件或bash脚本。两者都由服务器以相同的方式提供,但是当我下载bash脚本时,getContentLength()方法返回正确的内容长度,当我下载APK文件时,该方法返回-1 ...

这是我用来提供文件的PHP函数:

function send_file($file) {

    header('Content-Type: application/force-download; name="' . basename($file) . '"');
    header("Content-Transfer-Encoding: binary");
    header('Content-Length: ' . filesize($file));
    header('Content-Disposition: attachment; filename="' . basename($file) . '"');
    header('Expires: 0');
    header('Cache-Control: no-cache, must-revalidate');
    header('Pragma: no-cache');
    readfile($file);
    exit();

}

下载文件的Java代码:

URL requestUrl = new URL("http://download.myesmart.net/api.php?id=" + Tablet.ID + "&type=" + type);

// Start the connection to the server
if ((connection = (HttpURLConnection) requestUrl.openConnection()) == null) {

    Log.d("TAG", "Impossible to contact the server.");
    return null;

}

connection.connect();

// Check the HTTP code
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {

    Log.d("TAG", "Bad server response : " + connection.getResponseMessage());
    return null;

}

// Return -1 sometimes...
Log.d("TAG", "Content-Length : " + connection.getContentLength());

如果我检查此Web工具的HTTP标头字段:http://www.webconfs.com/http-header-check.php,我会得到相同的响应类型,但第一个有效,第二个没有。

HTTP/1.1 200 OK =>
Set-Cookie => mailplan=R243416773; path=/; expires=Thu, 13-Nov-2014 09:19:48 GMT
Content-Type => application/force-download; name="configure-keyboard.sh"
Server => Apache
Content-Transfer-Encoding => binary
Content-Disposition => attachment; filename="configure-keyboard.sh"
Expires => 0
Cache-Control => no-cache, must-revalidate
Pragma => no-cache
Vary => Accept-Encoding
Accept-Ranges => bytes
Date => Thu, 13 Nov 2014 08:19:39 GMT
Connection => close
X-Geo => varn16.rbx5
X-Geo-Port => 1015
X-Cacheable => Not cacheable: no-cache
Content-Length => 1118

HTTP/1.1 200 OK =>
Set-Cookie => mailplan=R1918955109; path=/; expires=Thu, 13-Nov-2014 08:59:12 GMT
Content-Type => application/force-download; name="InfoConfort_20141102_ec_v1dev.apk"
Server => Apache
Content-Transfer-Encoding => binary
Content-Disposition => attachment; filename="InfoConfort_20141102_ec_v1dev.apk"
Expires => 0
Cache-Control => no-cache, must-revalidate
Pragma => no-cache
Vary => Accept-Encoding
Accept-Ranges => bytes
Date => Thu, 13 Nov 2014 07:53:43 GMT
Connection => close
X-Geo => varn16.rbx5
X-Geo-Port => 1015
X-Cacheable => Not cacheable: no-cache
Content-Length => 5149910

任何想法???

1 个答案:

答案 0 :(得分:0)

根据JavaDocs,getContentLength方法返回-1,如果内容长度尚未知晓。

你介意试试这个:

URL requestUrl = new URL("http://download.myesmart.net/api.php?id=" + Tablet.ID + "&type=" + type);

//Start the connection to the server
if ((connection = (HttpURLConnection) requestUrl.openConnection()) == null) {

 Log.d("TAG", "Impossible to contact the server.");
 return null;

}   
connection.setRequestProperty("Accept-Encoding", "identity")
Log.d("TAG", "Content-Length : " + connection.getContentLength());

<强>编辑。

相关问题