java.net.ConnectException:通过代理连接时连接超时

时间:2012-10-09 08:22:59

标签: java url proxy download urlconnection

我正在尝试从URL下载zip文件,并使用java代码将其存储在本地系统中。我也使用系统代理。它无法连接到网址。有什么想法吗?

public static void main()
{
   try
   {
      long startTime = System.currentTimeMillis();

      System.out.println("Connecting to the url...\n");
      System.setProperty("http.proxyHost", " http://abc.com");
      System.setProperty("http.proxyPort","1111");

      URL url = new URL("http://sourceforge.net/projects/sevenzip/files/7-Zip/9.20/7z920.exe/download?use_mirror=nchc");
      url.openConnection();
      InputStream reader = url.openStream();
      FileOutputStream writer = new FileOutputStream("/home/user/result/apps.zip);
      byte[] buffer = new byte[153600];
      int totalBytesRead = 0;
      int bytesRead = 0;

      System.out.println("Reading ZIP file 150KB blocks at a time.\n");

      while ((bytesRead = reader.read(buffer)) > 0)
      {  
         writer.write(buffer, 0, bytesRead);
         buffer = new byte[153600];
         totalBytesRead += bytesRead;
      }

      long endTime = System.currentTimeMillis();

      System.out.println("Done downloading. " + (new Integer(totalBytesRead).toString()) + " bytes read (" + (new Long(endTime - startTime).toString()) + " millseconds).\n");
      writer.close();
      reader.close();
   }
   catch (MalformedURLException e)
   {
      e.printStackTrace();
   }
   catch (IOException e)
   {
      e.printStackTrace();
   }
   unZip(INPUT_ZIP_FILE);

}

我收到以下错误:

java.net.ConnectException: Connection timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:977)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:925)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
    at java.net.URL.openStream(URL.java:1010)
    at test.main(test.java:33)

当我尝试ping网址时,它会显示未知主机,但我可以ping网址,例如www.google.com

2 个答案:

答案 0 :(得分:6)

尝试使用page了解如何使用代理 - 看起来很全面。

答案 1 :(得分:3)

我认为你支持某些防火墙,因为你的代码在我的本地机器上运行得很好。我没有对您的代码进行任何更改。 zip文件在我的本地计算机上下载。控制台显示以下消息:

  

连接到网址...

     

一次读取ZIP文件150KB块。

     

完成下载。读取1110476个字节(13816毫秒)。