我遇到了以下问题: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#7168608。
- 如果有人可以帮助我编写代码,应该返回正确的内容长度以用于直接 FTP 连接、通过代理的 FTP 连接和本地
file:/
URL,我将不胜感激。 - 如果给定的问题不能用 JDK6 解决,建议任何其他绝对适用于我提到的所有情况的库(Apache Http Client?)。