public File attemptToDownload ( URL fileLocation ) throws URISyntaxException , IOException { String filename = FilenameUtils . getName ( fileLocation . getFile ( ) ) ; SocketConfig socketConfig = SocketConfig . custom ( ) . setSoTimeout ( fileDownloadReadTimeout ) . build ( ) ; RequestConfig requestConfig = RequestConfig . custom ( ) . setConnectTimeout ( fileDownloadConnectTimeout ) . build ( ) ; HttpClientBuilder httpClientBuilder = HttpClients . custom ( ) . setDefaultSocketConfig ( socketConfig ) . setDefaultRequestConfig ( requestConfig ) . disableContentCompression ( ) ; if ( useSystemProxy && proxyConfig . isProxyAvailable ( ) ) { LOG . info ( "Using http proxy: " + proxyConfig . getHost ( ) + ":" + proxyConfig . getPort ( ) ) ; httpClientBuilder . setProxy ( new HttpHost ( proxyConfig . getHost ( ) , proxyConfig . getPort ( ) ) ) ; } CloseableHttpClient httpClient = httpClientBuilder . build ( ) ; LOG . info ( "Downloading '" + filename + "'..." ) ; CloseableHttpResponse fileDownloadResponse = httpClient . execute ( new HttpGet ( fileLocation . toURI ( ) ) ) ; HttpEntity remoteFileStream = fileDownloadResponse . getEntity ( ) ; File fileToDownload = new File ( downloadDirectory + File . separator + filename ) ; try { copyInputStreamToFile ( remoteFileStream . getContent ( ) , fileToDownload ) ; } catch ( IOException ex ) { LOG . error ( "Problem downloading '" + filename + "'... " + ex . getCause ( ) . getLocalizedMessage ( ) ) ; fileToDownload = null ; } finally { fileDownloadResponse . close ( ) ; } return fileToDownload ; }
Attempt to download a file