以下代码在 Java 6(及更早版本)下可以正常工作,但在更新到 JRE 7(Java 7)后停止工作。
URL 是一个 FTP 文件:
ftp://ftp-private.ncbi.nlm.nih.gov/pubchem/.fetch/96/4133257873201306969.sdf.gz
这是我得到的输出:
应用程序/八位字节流 -1 [Ljava.lang.StackTraceElement;@5419f97c
这是我的代码:
public static void store(URL url, File targetFile){
try
{
System.out.println(url);
URLConnection uc = url.openConnection();
String contentType = uc.getContentType();
System.out.println(contentType);
int contentLength = uc.getContentLength();
System.out.println(contentLength);
Settings.setDownloadSize(contentLength);
if (contentType.startsWith("text/") || contentLength == -1) {
throw new IOException("This is not a binary file.");
}
InputStream raw = uc.getInputStream();
InputStream in = new BufferedInputStream(raw);
byte[] data = new byte[contentLength];
int bytesRead = 0;
StatusPanel.updateProgrssBar(bytesRead);
int offset = 0;
while (offset < contentLength) {
bytesRead = in.read(data, offset, data.length - offset);
if (bytesRead == -1) {
break;
}
offset += bytesRead;
StatusPanel.updateProgrssBar(offset);
}
in.close();
if (offset != contentLength) {
throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
}
FileOutputStream out = new FileOutputStream(targetFile);
out.write(data);
out.flush();
out.close();
//StatusPanel.setStatus("File has been stored at " + targetFile.toString());
//System.out.println("file has been stored at " + targetFile.toString());
}
内容长度返回 -1:
Area: API: Networking
Synopsis: Server Connection Shuts Down when Attempting to Read Data When http Response Code is -1
如何使此代码与 Java 7 兼容?
描述:由于 CR 6886436 的错误修复,HTTP 协议处理程序将关闭与发送响应但没有有效 HTTP 状态行的服务器的连接。发生这种情况时,任何尝试读取该连接上的数据都会导致 IOException。
例如,下面的代码是有问题的:
public static void test () throws Exception {
.....
HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
....
System.out.println ("Response code: " + urlc.getResponseCode());
/** Following line throws java.io.IOException: Invalid Http response
* when Response Code returned was -1
*/
InputStream is = urlc.getInputStream(); // PROBLEMATIC CODE
要解决此问题,请检查 getResponseCode 方法的返回值并适当处理 -1 值;也许通过打开一个新连接,或者在流上调用 getErrorStream。不兼容的性质:行为 RFE:7055058
问题肯定出在getContentLength()
方法上。
对于 JRE6,此方法返回一个值,但对于 JRE7,我得到 -1。