0

I want to download pdf file but source url is too slow. Because of this, I am giving connection timeout exception with following code.

    try {
            URL url = new URL(source);
            HttpURLConnection huc = (HttpURLConnection) 
            url.openConnection();
            huc.setConnectTimeout(0); //for unlimited
            huc.setReadTimeout(0);
            try (InputStream in = huc.getInputStream()) {
                Files.copy(in, Paths.get(destination), StandardCopyOption.REPLACE_EXISTING);
            }
        } catch (IOException e) {
            LOGGER.info("Error occured while copying file, %s", e);
            return false;
        }

I tried to change setConnectTimeout and setReadTimeout values both 0 and 5 minutes by converting to milliseconds but after the ~130 seconds, it throws connection timeout exception. I couldn't change this value. After the set read and connection timeout, getConnectionTimeout and getReadTimeout value return value, my set.

Also I tried apache libraries to copy input stream to destination but again I received connection timeout exception.

I have already known, connection bigger than 15 seconds is abnormal but I want to download file how long it takes.

So, how to increase this timeout value?

2
  • 2
    "I have already known, connection bigger than 15 seconds is abnormal but I want to download file how long it takes." -- The connect and read timeout values have nothing to do with how long a file takes to download. The connection timeout happens before a connection is established, long before the file starts to download. Once the connection is established the connection timeout value has no meaning. Commented Nov 4, 2017 at 6:21
  • Sorry for misunderstanding, "how long it takes" means total time of connection and read. Commented Nov 10, 2017 at 8:42

1 Answer 1

1

You cannot increase the connect timeout beyond the platform default (contrary to 20 years of Javadoc), and there is no point in doing so. It has no bearing on download times. The only relevant timeout for that is the read timeout.

In any case, if the 'source URL is too slow', merely setting timeouts won't make it any faster.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.