Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
InputStream打开问题:java.io.FileNotFoundException_Java_Url_Inputstream_Filenotfoundexception - Fatal编程技术网

InputStream打开问题:java.io.FileNotFoundException

InputStream打开问题:java.io.FileNotFoundException,java,url,inputstream,filenotfoundexception,Java,Url,Inputstream,Filenotfoundexception,从一天到下一天,当从URL对象返回InputStream时,我得到一个java.io.FileNotFoundException错误 这只会发生在我使用JSoup扫描的站点的一些URL上。URL地址是正确的,因为通过将地址复制到浏览器中,它会返回正确的页面 这是令人沮丧的,因为我没有对代码做任何更改。网站是否检测到我的页面扫描并阻止了我的请求 守则: public Document getDocument( String source ) { Document doc = nu

从一天到下一天,当从URL对象返回InputStream时,我得到一个java.io.FileNotFoundException错误

这只会发生在我使用JSoup扫描的站点的一些URL上。URL地址是正确的,因为通过将地址复制到浏览器中,它会返回正确的页面

这是令人沮丧的,因为我没有对代码做任何更改。网站是否检测到我的页面扫描并阻止了我的请求

守则:

    public Document getDocument( String source ) {

    Document doc = null;
    InputStream is = null;
    URL newUrl = null;
    try {
        newUrl = new URL( source );
        is = newUrl.openStream();
        Connection conn = Jsoup.connect( source );
        conn.timeout( 0 );
        doc = Jsoup.parse( is, "CP1252", source );
    }
    catch ( IOException ioe ) {
        ioe.printStackTrace();
    }

    return doc;
}
错误:

java.io.FileNotFoundException: http://www.ratebeer.com/breweries/antigua-barbuda/0/9/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at beerparser.web.RateBeerParser.getDocument(RateBeerParser.java:389)
第389行是InputStream对象分配。

您可以尝试:

URLConnection connection = new URL(url).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();

当您收到错误响应代码时,请记住,您无法使用
getInputStream()


在这种情况下,您必须使用
getErrorStream()

读取返回的正文。当我尝试您的URL时,它不会返回任何内容。您确定浏览器中没有缓存中的页面(请在页面上尝试CTRL+F5以确保),并且该页面实际上不起作用吗?我发现该页面似乎起作用:。你能试试这个URL来确认你的代码仍然有效吗。URL地址中带有“/0/”的链接似乎不起作用。是的,是缓存。目前,每个带有“/0/”的url都返回空白页。保险杠谢谢朱利安。我正在更改查询字符串以暂时避免该问题。