JSoup随机抛出java.io.IOException:当从浏览器运行时,流被关闭

时间:2013-05-14 18:47:33

标签: javafx jsoup

从浏览器运行我的JavaFX应用程序时(或作为web-start),我遇到了一些奇怪的JSoup问题。

当我从IDE(Eclipse或Netbeans)内部运行或作为独立应用程序运行时,它会正常运行。当我尝试以网络启动或浏览器(Chrome)运行时,JSoup会随机抛出“java.io.IOException:stream is closed”。

我正在尝试解析的网站是thepiratebay.sx。当我第一次运行应用程序(从浏览器)时,我收到此错误。在应用程序运行的情况下,如果我再次尝试解析,那么它有效...有时候。

JSoup代码:

try {
    //TODO: Change to HttpFetcher. This method is reporting "stream is closed" when running on browser
    Connection con = Jsoup.connect(url)
            .timeout(HTTP_TIMEOUT)
            .userAgent(UserAgentGenerator.getUserAgent())
            .followRedirects(false);
    doc = con.get();
    System.out.println("Fetching... " + url);
} catch (IOException e) {
    e.printStackTrace();
    System.out.println("Parser connect must have timed out, no results. " + url);
    fetchFailed[i] = true;
    continue;
}       
finally {
    i++;
    if (CommonTFUtils.isAllTrue(fetchFailed)) {
        throw new HttpException("Fetcher failed on every URL of " + response.getSite_name());
    }
}

抛出异常:

CacheEntry[http://thepiratebay.sx/browse/207/0/7]: updateAvailable=true,lastModified=Tue May 14 14:28:16 BRT 2013,length=-1
java.io.IOException: stream is closed
    at sun.net.www.http.ChunkedInputStream.ensureOpen(Unknown Source)
    at sun.net.www.http.ChunkedInputStream.read(Unknown Source)
    at java.io.FilterInputStream.read(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.close(Unknown Source)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:468)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
    at com.package.torrent.parser.GenericParser.search(GenericParser.java:147)
    at com.package.torrent.parser.GenericParser.browse(GenericParser.java:82)
    at com.package.search.TrackerSearch.searchTracker(TrackerSearch.java:69)
    at com.package.search.TrackerSearch.searchAllTrackers(TrackerSearch.java:40)
    at com.package.search.TrackerSearch.searchAllTrackers(TrackerSearch.java:23)
    at com.package.search.MovieBrowser.browseTrackers(MovieBrowser.java:49)
    at com.package.ui.browse.BrowseController$MovieBrowserTask.call(BrowseController.java:237)
    at com.package.ui.browse.BrowseController$MovieBrowserTask.call(BrowseController.java:213)
    at javafx.concurrent.Task$TaskCallable.call(Task.java:1259)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

有没有人知道可能导致这种情况的原因?

提前致谢。

1 个答案:

答案 0 :(得分:2)

我想我找到了解决方案。在调用JSoup之前放置此代码。显然,applet和web start将此值设置为true。现在,我想知道为什么Sun会强迫您非静态地访问静态变量。

new URL("jar:file://dummy.jar!/").openConnection().setDefaultUseCaches(false);

当缓存URL时,JSoup处理不好并将其视为异常。