dma*_*a_k 5 java proxy urlconnection http-proxy
我遇到了以下问题:当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'
Run Code Online (Sandbox Code Playgroud)
在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);
Run Code Online (Sandbox Code Playgroud)
和我-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);
Run Code Online (Sandbox Code Playgroud)
输出:
Got length (1): -1
Got length (2): -1 but wanted 30745
Run Code Online (Sandbox Code Playgroud)
它似乎是JDK6中的一个错误,所以我打开了新的bug#7168608.
file:/URL的FTP连接,我将不胜感激.我要做的一件事是实际阅读响应(写下我的头顶,所以预计会出现错误):
URLConnection connection= url.openConnection();
InputStream input= connection.getInputStream();
byte[] buffer= new byte[4096];
while(input.read(buffer) > 0)
;
logger.debug("Got length: " + getContentLength());
Run Code Online (Sandbox Code Playgroud)
如果您获得的大小合适,请寻找一种方法使 URLConnection 读取标头而不是数据,以避免读取整个响应。