1

I am getting Connection timed out: connect exception in java code please see below. I have searched in google but haven't got much help,U can run this code in your machine,its complete code I am giving below. code-

public class download {
    // final static int size=1024;

    public static void downloadValuationPDFReport() {
        OutputStream outStream = null;
        URLConnection uCon = null;
        InputStream is = null;
        String fAddress = null;
        URL Url = null;
        String localFileName = "abc.zip";
        String destinationDir = "H:\\";//"C:\\Users\\501301605\\Downloads";
        try {
            fAddress = "http://www.novell.com/coolsolutions/tools/downloads/ntradping.zip";
            byte[] buf;
            int byteRead = 0;
            Url = new URL(fAddress);
            outStream = new BufferedOutputStream(new FileOutputStream(destinationDir + "\\" + localFileName));
            uCon = Url.openConnection();
            is = uCon.getInputStream();
            buf = new byte[1024];
            while ((byteRead = is.read(buf)) != -1) {
                outStream.write(buf, 0, byteRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                outStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Lrrr
  • 4,755
  • 5
  • 41
  • 63
RSingh
  • 171
  • 2
  • 12
  • Can you try setting some connect time out: uCon .setConnectTimeout(VALUE); uCon.setReadTimeout(VALUE); – Saqib Rezwan May 27 '15 at 09:29
  • @SaqibRezwan thanks for the comment,but getting same problem – RSingh May 27 '15 at 09:33
  • 3
    Works for me properly. Firewall problem? Can you download the file directly (from a browser)? – agad May 27 '15 at 09:36
  • Are you behind a proxy or firewall perchance? – Anders R. Bystrup May 27 '15 at 09:37
  • @agad have it downloaded the file properly? – RSingh May 27 '15 at 09:37
  • @RSingh yes, file was downloaded. – agad May 27 '15 at 09:37
  • @AndersR.Bystrup yes behind proxy,but url that is mentioned in the above code is easily accessible from browser,then why it will be blocked through code? – RSingh May 27 '15 at 09:38
  • 1
    Why do you think that Java automatically know your browsers proxy settings? – Anders R. Bystrup May 27 '15 at 09:39
  • @agad thank you very much,but I don't know why I am not able to get it though,is it could be proxy issue,but link given in code is accessible from browser – RSingh May 27 '15 at 09:40
  • Just ran your code, it worked perfectly. So, there may be some restrictions in windows firewall. Create a new out going rule in windows firewall which allow everything. Hopefully, You will be able to download. – Saqib Rezwan May 27 '15 at 09:40
  • As @AndersR.Bystrup has written: java doesn't know your proxy settings. See http://stackoverflow.com/questions/8264889/proxy-with-java-urlconnection-class for solution. – agad May 27 '15 at 09:42

1 Answers1

2

It is very likely that you are within a network where you are not allowed to connect directly to anything port 80; try and:

telnet www.novell.com 80

and see if you get an answer; this will probably result in a timeout as well.

More likely than not you need to use a proxy (see here for instance). Also, your code leaves many resources dangling, and you use File which is obsolete.

Here is how to do it in modern code:

final Path dstfile = Paths.get("h:", "abc.zip");

// ...

try (
    final InputStream in = url.openStream();
) {
    Files.copy(in, dstfile, StandardOpenOption.CREATE_NEW);
}
Community
  • 1
  • 1
fge
  • 119,121
  • 33
  • 254
  • 329
  • yes getting could not connect to the host .then how to use proxy seetings?can you help on that plzz? – RSingh May 27 '15 at 09:41
  • Edited an answer to add a link for that; try and search around as well. – fge May 27 '15 at 09:43