以下代码在JavaSE 6中运行良好,但在JavaSE 7中执行时会抛出ConnectException (超时)。这是JDK7错误还是错误代码?我真的不明白。
public static void main(String[] args) {
try {
URL url = new URL("http://dl.dropbox.com/u/34206572/version.txt");
url.openConnection().connect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}发布于 2012-01-21 00:38:32
我在1.7.0_02-b13中尝试了这段代码,它工作得很好。我访问上面的链接,它是不可用的(返回第404页)。
也许,您的意思是以下代码崩溃:
public static void main(String[] args) throws Exception {
URL url = new URL("http://dl.dropbox.com/u/34206572/version.txt");
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
}有以下异常(我格式化了它):
Exception in thread "main" java.io.FileNotFoundException:
http://dl.dropbox.com/u/34206572/version.txt
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(
HttpURLConnection.java:1610)https://stackoverflow.com/questions/8162068
复制相似问题