【发布时间】:2012-03-07 18:47:55
【问题描述】:
我遇到了以下问题:当通过代理使用URLConnection 时,内容长度始终设置为-1。
首先我检查了代理确实返回了Content-Length(lynx 和wget 也通过代理工作;没有其他方法可以从本地网络访问互联网):
$ lynx -source -head ftp://ftp.wipo.int/pub/published_pct_sequences/publication/2003/1218/WO03_104476/WO2003-104476-001.zip
HTTP/1.1 200 OK
Last-Modified: Mon, 09 Jul 2007 17:02:37 GMT
Content-Type: application/x-zip-compressed
Content-Length: 30745
Connection: close
Date: Thu, 02 Feb 2012 17:18:52 GMT
$ wget -S -X HEAD ftp://ftp.wipo.int/pub/published_pct_sequences/publication/2003/1218/WO03_104476/WO2003-104476-001.zip
--2012-04-03 19:36:54-- ftp://ftp.wipo.int/pub/published_pct_sequences/publication/2003/1218/WO03_104476/WO2003-104476-001.zip
Resolving proxy... 10.10.0.12
Connecting to proxy|10.10.0.12|:8080... connected.
Proxy request sent, awaiting response...
HTTP/1.1 200 OK
Last-Modified: Mon, 09 Jul 2007 17:02:37 GMT
Content-Type: application/x-zip-compressed
Content-Length: 30745
Connection: close
Age: 0
Date: Tue, 03 Apr 2012 17:36:54 GMT
Length: 30745 (30K) [application/x-zip-compressed]
Saving to: `WO2003-104476-001.zip'
我在 Java 中写道:
URL url = new URL("ftp://ftp.wipo.int/pub/published_pct_sequences/publication/2003/1218/WO03_104476/WO2003-104476-001.zip");
int length = url.openConnection().getContentLength();
logger.debug("Got length: " + length);
我得到-1。我开始调试FtpURLConnection,结果发现必要的信息在底层HttpURLConnection.responses 字段中,但它从未从那里正确填充:
(标题中有Content-Length: 30745)。当您开始读取流时,甚至在读取流之后,内容长度都不会更新。代码:
URL url = new URL("ftp://ftp.wipo.int/pub/published_pct_sequences/publication/2003/1218/WO03_104476/WO2003-104476-001.zip");
URLConnection connection = url.openConnection();
logger.debug("Got length (1): " + connection.getContentLength());
InputStream input = connection.getInputStream();
byte[] buffer = new byte[4096];
int count = 0, len;
while ((len = input.read(buffer)) > 0) {
count += len;
}
logger.debug("Got length (2): " + connection.getContentLength() + " but wanted " + count);
输出:
Got length (1): -1
Got length (2): -1 but wanted 30745
好像是JDK6的bug,所以我新开了bug#7168608。
- 如果有人可以帮助我编写代码,应返回正确的内容长度以用于直接 FTP 连接、通过代理的 FTP 连接和本地
file:/URL,我将不胜感激。 - 如果给定的问题不能用 JDK6 解决,建议任何其他绝对适用于我提到的所有情况的库 (Apache Http Client?)。
【问题讨论】:
-
为什么需要内容长度?实际的数据流是否正确?如果是这样,您不需要内容长度,并且一切正常。
-
@jtahlborn:实际 URL 是正确的(它是公共 FTP,因此您也可以测试)。我需要了解内容长度而不将流读到最后,这显然是可以做到的。
标签: java proxy urlconnection http-proxy