【问题标题】:JavaSE 7 URL Timing out that works in JavaSE 6?Java SE 7 URL 超时在 Java SE 6 中有效?
【发布时间】:2011-11-17 04:19:30
【问题描述】:

以下代码在 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();
    }
    }

【问题讨论】:

  • 我刚刚尝试了这段代码,它在 1.7.0 和 1.6.0_25 中都运行良好。您使用的是什么版本的 Java?

标签: java networking timeout java-7


【解决方案1】:

我在 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    相关资源
    最近更新 更多