问题描述
我想知道该文件的长度,所以我尝试 getContentLength()
。它正常工作与网络连接(EDGE / 3G),但将返回-1,无线网络?
为什么?支持WiFi是好的,找到文件,它可以下载,但返回getContentLength()
始终为-1。我不明白。 文件
是一个谷歌的文档文件。
是否有其他方式来获得的长度?
我的code是:
网址URL =新的URL(文件);
URLConnection的体conexion = url.openConnection();
conexion.connect();
INT POIDS = conexion.getContentLength();
服务器可能发送回被分块HTTP响应。
的getContentLength()方法的行为是返回的内容的长度的内部的值,即提供给它。当客户端收到一个HTTP分块响应,响应的长度是不知道的,因此内容长度值标记为-1。
响应的分块性质可以通过传输编码
头值确定;分块响应具有分块值。 HTTP服务器不需要提供一个的Content-Length如果响应是通过分块编码发送
头值;事实上,服务器鼓励不发送Content-Length头的分块响应,客户端应该忽略Content-Length头。
至于为什么服务器响应不同的方式在两个网络的实际原因,以及它取决于各种因素。一般的服务器会选择更优化的传送模式,根据客户端的性质。出于某种原因,它已检测到它是最好一种类型的连接发送分块响应。答案可能在于HTTP请求头,但未必如此。
I want to know the length of the file, so I tried getContentLength()
. It works fine with network connection (edge/3g) but returns -1 with WiFi?
Why? The WiFi is good and the file was found, it can be downloaded but the return of getContentLength()
is always "-1". I dont understand. file
is a google documents file.
Is there an other way to get the length?
My code is:
URL url = new URL(file);
URLConnection conexion = url.openConnection();
conexion.connect();
int poids = conexion.getContentLength();
The server is probably sending back a HTTP response that is chunked.
The behavior of the getContentLength() method is to return the 'internal' value of the length of the content, that is available to it. When the client receives a HTTP chunked response, the length of the response is not known, and hence the content length value is marked as -1.
The chunked nature of the response can determined by the Transfer-Encoding
header value; chunked responses have a value of chunked. HTTP servers need not provide a Content-Length
header value if the response is sent via chunked encoding; in fact, servers are encouraged to not send the Content-Length header for a chunked response, for the client is supposed to ignore the Content-Length header.
As for the actual reason on why the server is responding differently in two networks, well it depends on various factors. Usually servers will opt for a more optimal delivery mode, depending on the nature of the client. For some reason, it has detected that it is better off sending chunked responses for one type of a connection. The answer might lie in the HTTP request headers, but not necessarily so.
这篇关于getContentLength()返回-1仅在无线网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!