Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.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
Java URL Java.net.ConnectException错误(Ping工作,其他URL工作)_Java_Url_Connection - Fatal编程技术网

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

Java URL Java.net.ConnectException错误(Ping工作,其他URL工作),java,url,connection,Java,Url,Connection,我正在编写一个程序,从列表中读取一些ID,从中找出不同的URL,并将图像保存到我的C:驱动器中 如果我在浏览器中导航到这些图像URL,它们就会工作。此外,如果我尝试从不同的服务器将URL链接到图像,这个程序完全可以工作。问题是,当我试图连接到这个URL上的特定图像时,它似乎不起作用。奇怪的是,当我ping这个URL时,我得到了0%的数据包丢失。此外,我的浏览器没有使用特殊的代理设置,这会导致它在Java程序上工作 以下错误/输出的原因可能是什么 Job started ... 1) AC_0A4

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

如果我在浏览器中导航到这些图像URL,它们就会工作。此外,如果我尝试从不同的服务器将URL链接到图像,这个程序完全可以工作。问题是,当我试图连接到这个URL上的特定图像时,它似乎不起作用。奇怪的是,当我ping这个URL时,我得到了0%的数据包丢失。此外,我的浏览器没有使用特殊的代理设置,这会导致它在Java程序上工作

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

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)
相应代码如下:

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
  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;

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

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

尝试使用

  Apache HttpComponents

关于

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

尝试使用

  Apache HttpComponents

关于编辑2

您的代码在读取循环中出错。不必返回正在接收的整个字节流。(可能还没有全部可用。)按照您的编码方式,输入流通常会提前关闭。这会导致与您发布的错误不同的错误(“流关闭”)

在任何情况下,在修复了您的循环之后,我能够运行您的代码来下载此图像,而没有任何问题

原创

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

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

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

编辑2

您的代码在读取循环中出错。不必返回正在接收的整个字节流。(可能还没有全部可用。)按照您的编码方式,输入流通常会提前关闭。这会导致与您发布的错误不同的错误(“流关闭”)

在任何情况下,在修复了您的循环之后,我能够运行您的代码来下载此图像,而没有任何问题

原创

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

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

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

如果web浏览器没有问题,并且应用程序出现问题,请尝试向java请求添加用户代理,如下所示:

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
  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;

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

当您的web浏览器没有问题且应用程序出现问题时,请尝试向java请求添加用户代理,如下所示:

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
  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;

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

“似乎不起作用”没有太大帮助。请告诉我们您得到了什么,如果有任何异常,请将stacktrace放在此处。对此表示抱歉。已修复。有许多与服务器要求相关的可能性。您可以发布您遇到问题的实际服务器URL吗?请澄清,您的意思是“cfmedia.vfmleonardo.com/imageRepo”还是“www.cfmedia.vfmlearnardo.com/imageRepo”?第一个URL无法解析。另外,我相信这是一个Cloudfront服务器,对吗?“似乎不工作”不是很有帮助。请告诉我们您得到了什么,如果有任何异常,请将stacktrace放在这里。对此表示抱歉。已修复。有许多与服务器要求相关的可能性。您可以发布您遇到问题的实际服务器URL吗?请澄清,您的意思是“cfmedia.vfmleonardo.com/imageRepo”还是“www.cfmedia.vfmlearnardo.com/imageRepo”?第一个URL无法解析。另外,我相信这是一个Cloudfront服务器,对吗?我试过了。请参阅OP中的编辑。不过感谢您的建议。尝试过了。请看作品中的编辑。不过谢谢你的建议。啊,我明白了。除代理问题外,这似乎也是原因。谢谢你的帮助。我相应地修复了这个循环,所以现在它似乎工作了。啊,我明白了。除代理问题外,这似乎也是原因。谢谢你的帮助。我相应地修复了循环,所以现在它似乎可以工作了。