6

I am trying to download a zip file from a URL and store it in the local system using java code. I am also using system proxy for the same. Its unable to connect to the url. Any idea?

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

}

i get the following error:

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)

It says unknown host when i try to ping the url, but i am able to ping urls like www.google.com

Zarkonnen
  • 22,200
  • 14
  • 65
  • 81
Chirag
  • 341
  • 2
  • 7
  • 14
  • if you cannot ping how can you connect to ? Does the url work from a web browser ? – BigMike Oct 09 '12 at 08:30
  • I've just checked what happens when I openConnection to that URL on my machine, and I can read data just fine. This suggests your computer is somehow unable to talk to sourceforge.net. As @BigMike suggests, see if you can actually visit that URL in a browser. – Zarkonnen Oct 09 '12 at 08:32
  • @BigMike yeah, the url works from a web browser – Chirag Oct 09 '12 at 08:32
  • 2
    So you can't ping the host, but you can browse to it? Is your browser set up to run through a proxy? – Zarkonnen Oct 09 '12 at 08:34
  • @Zarkonnen I suspect the very same due to the setting of http.proxyHost, btw is http://abc.com actually an http proxy ? – BigMike Oct 09 '12 at 08:35
  • @Zarkonnen i am specifying the proxy settings here in my code – Chirag Oct 09 '12 at 08:36
  • @BigMike no, abc.com is just an example – Chirag Oct 09 '12 at 08:36
  • @user1729154 check your proxy settings then, if the browser works there's have to be something involved with that. Are you trying your code inside some dev env tool (e.g. Eclipse) ? – BigMike Oct 09 '12 at 08:38
  • i am running the code from linux – Chirag Oct 09 '12 at 08:40
  • @BigMike when tried from eclipse, it runs perfectly fine – Chirag Oct 09 '12 at 08:41
  • You can try setting this System.setProperty("java.net.useSystemProxies", "true"); like explained http://www.rgagnon.com/javadetails/java-0085.html – BigMike Oct 09 '12 at 08:41
  • @user1729154 Apologies for failing to read the bit about proxies. – Zarkonnen Oct 09 '12 at 08:41
  • i am able to download the file using wget but still no luck with the java code – Chirag Oct 09 '12 at 09:44

2 Answers2

7

Try this page on how to work with proxies - looks comprehensive.

RonK
  • 9,472
  • 8
  • 51
  • 87
3

I think you are behind some firewall because your code is running perfectly in my local machine. I have not done any changes in your code. The zip file is downloaded in my local machine. The console shows this message:

Connecting to the url...

Reading ZIP file 150KB blocks at a time.

Done downloading. 1110476 bytes read (13816 millseconds).

bluish
  • 26,356
  • 27
  • 122
  • 180
Tarun Jain
  • 262
  • 1
  • 8