Java URL java.net.ConnectException 错误(Ping 工作,其他 URL 工作)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11777643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:19:53  来源:igfitidea点击:

Java URL java.net.ConnectException Error (Ping works, and other URLs work)

javaurlconnection

提问by ns1

I am writing a program that reads some IDs from a list, figures out different URLs from them, and saves the images to my C: drive.

我正在编写一个程序,从列表中读取一些 ID,从中找出不同的 URL,并将图像保存到我的 C: 驱动器。

The image URLs work if I navigate to them in my browser. Additionally, if I try URLs to images from a different server this program works entirely. The issue is when I try to connect to specific images at this URL it does not seem to work. The strange thing is when I ping the URL I get 0% packet loss. Additionally, my browser is not using special proxy settings that would cause it to work over the Java program.

如果我在浏览器中导航到图像 URL,则图像 URL 有效。此外,如果我尝试使用来自不同服务器的图像的 URL,则该程序完全可以正常工作。问题是当我尝试连接到此 URL 上的特定图像时,它似乎不起作用。奇怪的是,当我 ping URL 时,我得到 0% 的丢包率。此外,我的浏览器没有使用会导致它在 Java 程序上工作的特殊代理设置。

What could be the cause of the below error/output?

以下错误/输出的原因可能是什么?

Job started ...
1) AC_0A47_EXT1.jpg 2/0/47/307/398/HA47H01_H.jpg
Exception #3 java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at pullVFMImagesFromVFMURLs.writeImage(pullVFMImagesFromVFMURLs.java:99)
at pullVFMImagesFromVFMURLs.pullvfmimagesfromvfmurls(pullVFMImagesFromVFMURLs.java:48)
at pullVFMImagesFromVFMURLs.main(pullVFMImagesFromVFMURLs.java:21)

More info:

更多信息:

The ID list (MissingImagez.txt) is as follows:

ID列表(MissingImagez.txt)如下:

AC_0A47_EXT1.jpg|2/0/47/307/398/xxx.jpg
AC_0C09_EXT1.jpg|3/0/44/130/589/xyz.jpg
AC_0C16_RM1.jpg|3/0/44/602/895/zzz.jpg
AC_0C17_BAR1.jpg|3/0/45/284/10/www.jpg

and the corresponding code is as follows:

对应的代码如下:

  public static void main(String[] args)
  {
    pullImagesFromURLs processor = new pullImagesFromURLs();

    String passParam = null;

    for (int i = 0; i < args.length; i++)
       passParam = (args[i].toString().toUpperCase());

    processor.pullvfmimagesfromvfmurls(passParam);
  }

  public void pullvfmimagesfromvfmurls(String passParam)
  {
    System.out.println("Job started ...");

    try
    {     
       boolean bOK = false;
       String sOrigName = "";
       String sURL = ""; 
       int iCount = 0;
       int iInt = 0;

       try
       {
          BufferedReader in = new BufferedReader(new FileReader("/tmp/missingImagez.txt"));
          String str;

          while ((str = in.readLine()) != null)
          {       
             iInt = str.indexOf("|");     
             sOrigName = str.substring(0,iInt);
             sURL = str.substring(iInt+1);
             iCount ++;
             System.out.println(iCount + ") " + sOrigName + " " + sURL);
             bOK = writeImage(sOrigName, sURL);         
          }           

          try
          {
             if (in != null)
                in.close();             
          }      
          catch (Exception e)
          {
          }

       }
       catch (IOException e)
       {
         System.out.println("Exception #1 " + e);
       }
    }

    catch (Exception e)
    {
       System.out.println("Exception #2 " + e);
       System.exit(-666);
    }

    System.out.println("Job completed");

    System.exit(-666);

  } 
 private static boolean writeImage(String origName, String vfmURL) 
  {

    boolean bOK = true;
    String sPath = "C:/images/img2754/";

    try
    {
       final String vfmURLPrefix = new String("http://www.blah.blah2.com/imageRepo/");

       BufferedInputStream stream = null; 
       FileOutputStream destination = null;
       URLConnection urlc = null;
       int readSize = 1024 * 50;
       int bytes_read;
       URL url = null;

        url = new URL(vfmURLPrefix + vfmURL);
        urlc = url.openConnection();
        urlc.setDoOutput(true);
        stream = new BufferedInputStream(url.openStream());
        System.out.println(url.toString());
        destination = new FileOutputStream(sPath+origName);
        byte[] buffer = new byte[readSize];

        while(true)
        {       

           bytes_read = stream.read(buffer);
           if (bytes_read == -1) {break;}
             destination.write(buffer, 0, bytes_read);
             stream.close(); 
        }

        if(stream != null)
            stream.close();

        if(destination != null) 
           destination.close();

    }

    catch(Exception e)
    {
       System.out.println("Exception #3 " + e);
    }
    return bOK;

  }

Tried using Apache HttpComponents. Get the following output: Okay -- I tried that...now I get:

尝试使用 Apache HttpComponents。得到以下输出:好的——我试过了……现在我得到了:

Exception #3     org.apache.http.conn.HttpHostConnectException: Connection to http://www.blah.blah2.com/ refused. 
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:190)
        at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
        at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
        at pullVFMImagesFromVFMURLs.writeImage(pullVFMImagesFromVFMURLs.java:100)
        at pullVFMImagesFromVFMURLs.pullvfmimagesfromvfmurls(pullVFMImagesFromVFMURLs.java:55)
        at pullVFMImagesFromVFMURLs.main(pullVFMImagesFromVFMURLs.java:28)
    Caused by: java.net.ConnectException: Connection timed out: connect
        at java.net.DualStackPlainSocketImpl.connect0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:127)
        at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
        ... 9 more

采纳答案by kaliatech

EDIT 2

编辑 2

Your code has an error in the read loop. InputStream.readdoes not necessary return the entire byte stream being received. (It might not all be available yet.) The way you have coded it, the input stream is going to often be prematurely closed. That results in a different error ("stream closed") than what you have posted though.

您的代码在读取循环中出错。 InputStream.read不需要返回正在接收的整个字节流。(可能还不是全部可用。)按照您的编码方式,输入流通常会过早关闭。这会导致与您发布的内容不同的错误(“流关闭”)。

In any case, after fixing your loop, I was able to run your code to download this image without any problems.

无论如何,在修复您的循环后,我能够运行您的代码来下载此图像而没有任何问题。

Original

原来的

As an aside, your code is a kind-of confusing. In particular, your code creates a URLConnection, but opens the stream directly from the URL. The URLConnectionis basically unused. It's also confusing because you call setDoOutput, but not setDoInput. I would rewrite your code more like this:

顺便说一句,您的代码有点令人困惑。特别是,您的代码创建了一个 URLConnection,但直接从 URL 打开流。该URLConnection的基本上是不用的。这也令人困惑,因为您调用了 setDoOutput,而不是 setDoInput。我会更像这样重写你的代码:

url = new URL(vfmURLPrefix + vfmURL);
urlc = url.openConnection();
stream = new BufferedInputStream(urlc.getInputStream()); // connect/request will be made.

I doubt that will solve your current problem though. To debug your current problem, I would recommend using Firebug or Chrome's Developer Tools to sniff the network connection/request/response when you request the same image URL in your browser. You might find unexpected redirects, status codes, cookies, or similar. With that extra knowledge in hand, you then might try casting the connection to an HttpURLConnectionso that you have more options available for customizing the request.

我怀疑这会解决您当前的问题。为了调试您当前的问题,当您在浏览器中请求相同的图像 URL 时,我建议使用 Firebug 或 Chrome 的开发人员工具来嗅探网络连接/请求/响应。您可能会发现意外的重定向、状态代码、cookie 或类似内容。掌握了这些额外知识后,您可以尝试将连接转换为HttpURLConnection,以便您有更多选项可用于自定义请求。

回答by vivek_jonam

When its fine with your web browser and a problem with your application, try adding user-agent to the java request, as follows:

当您的 Web 浏览器正常并且您的应用程序出现问题时,请尝试将用户代理添加到 Java 请求中,如下所示:

request.setHeader("User-Agent","Mozilla/3.0");

回答by Grim

There can be a few problems: 1. You must be logged in. 2. You have to have a Cookie to get the Images 3. The request of URL() does not match a browser-request. ...

可能有几个问题: 1. 您必须登录。 2. 您必须有一个 Cookie 才能获取图像 3. URL() 的请求与浏览器请求不匹配。...

Try to simulate a real browser using the

尝试使用

  Apache HttpComponents

Regards

问候