1

So basically I'm writing a program which outputs a website. The website should be written directly onto the server.

I am using this code to write to the server:

URL  url = new URL("ftp://usr:pw@ftp.site.com/test/login.php;type=i");
    URLConnection urlc = url.openConnection();
    OutputStream os = urlc.getOutputStream(); // To upload
    OutputStream buffer = new BufferedOutputStream(os);
    PrintStream output = new PrintStream(buffer);
    output.print(sb);

But sometimes my program runs and never terminates, so I'm guessing the connection is not being successful. '

Could I create a timeout in the case that this occurs? ie like 30 seconds, the connection would be automatically terminated so that the program could terminate.

Thanks in advance!

malteser
  • 485
  • 2
  • 10
  • 27

2 Answers2

2
con.setConnectTimeout(5000);//5 seconds
con.setReadTimeout(5000);
2

URLConnection has a setConnectTimeout method. By using this method you can set the time:

urlc.setConnectTimeout(30); // Time is in milliseconds
urlc.setReadTimeout(30);
Kanagaraj M
  • 956
  • 8
  • 18