Example usage for java.net URLConnection setReadTimeout

List of usage examples for java.net URLConnection setReadTimeout

Introduction

In this page you can find the example usage for java.net URLConnection setReadTimeout.

Prototype

public void setReadTimeout(int timeout) 

Source Link

Document

Sets the read timeout to a specified timeout, in milliseconds.

Usage

From source file:com.clark.func.Functions.java

/**
 * Copies bytes from the URL <code>source</code> to a file
 * <code>destination</code>. The directories up to <code>destination</code>
 * will be created if they don't already exist. <code>destination</code>
 * will be overwritten if it already exists.
 * /*w w  w. j  a  v a2 s. com*/
 * @param source
 *            the <code>URL</code> to copy bytes from, must not be
 *            <code>null</code>
 * @param destination
 *            the non-directory <code>File</code> to write bytes to
 *            (possibly overwriting), must not be <code>null</code>
 * @param connectionTimeout
 *            the number of milliseconds until this method will timeout if
 *            no connection could be established to the <code>source</code>
 * @param readTimeout
 *            the number of milliseconds until this method will timeout if
 *            no data could be read from the <code>source</code>
 * @throws IOException
 *             if <code>source</code> URL cannot be opened
 * @throws IOException
 *             if <code>destination</code> is a directory
 * @throws IOException
 *             if <code>destination</code> cannot be written
 * @throws IOException
 *             if <code>destination</code> needs creating but can't be
 * @throws IOException
 *             if an IO error occurs during copying
 * @since Commons IO 2.0
 */
public static void copyURLToFile(URL source, File destination, int connectionTimeout, int readTimeout)
        throws IOException {
    URLConnection connection = source.openConnection();
    connection.setConnectTimeout(connectionTimeout);
    connection.setReadTimeout(readTimeout);
    InputStream input = connection.getInputStream();
    copyInputStreamToFile(input, destination);
}