List of usage examples for org.apache.commons.httpclient.params HttpMethodParams WARN_EXTRA_INPUT
String WARN_EXTRA_INPUT
To view the source code for org.apache.commons.httpclient.params HttpMethodParams WARN_EXTRA_INPUT.
Click Source Link
From source file:net.paissad.waqtsalat.utils.DownloadHelper.java
/** * <p>/* ww w .ja v a 2 s . com*/ * Download a resource from the specified url and save it to the specified * file. * </p> * <b>Note</b>: If you plan to cancel the download at any time, then this * method should be embed into it's own thread. * <p> * <b>Example</b>: * * <pre> * final DownloadHelper downloader = new DownloadHelper(); * Thread t = new Thread() { * public void run() { * try { * downloader.download("http://domain.com/file.zip", new File("/tmp/output.zip")); * } catch (Exception e) { * ... * } * } * }; * t.start(); * // Waits 3 seconds and then cancels the download. * Thread.sleep(3 * 1000L); * downloader.cancel(); * ... * </pre> * * </p> * * @param url * - The url of the file to download. * @param file * - The downloaded file (where it will be stored). * @throws IOException * @throws HttpException */ public void download(final String url, final File file) throws HttpException, IOException { GetMethod method = null; InputStream responseBody = null; OutputStream out = null; try { final boolean fileExisted = file.exists(); HttpClient client = new HttpClient(); method = new GetMethod(url); method.setFollowRedirects(true); method.setRequestHeader("User-Agent", WSConstants.WS_USER_AGENT); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); method.getParams().setParameter(HttpMethodParams.WARN_EXTRA_INPUT, Boolean.TRUE); // Execute the method. int responseCode = client.executeMethod(method); if (responseCode != HttpStatus.SC_OK) { logger.error("Http method failed : {}.", method.getStatusLine().toString()); } // Read the response body. responseBody = method.getResponseBodyAsStream(); // Let's try to compute the amount of total bytes of the file to // download. URL u = new URL(url); URLConnection urlc = u.openConnection(); this.totalBytes = urlc.getContentLength(); long lastModified = urlc.getLastModified(); // The OutputStream for the 'downloaded' file. out = new BufferedOutputStream(new FileOutputStream(file)); this.updateState(DownloadState.IN_PROGRESS); byte[] data = new byte[BUFFER_SIZE]; int length; while ((length = responseBody.read(data, 0, BUFFER_SIZE)) != -1 && !isCancelled) { out.write(data, 0, length); this.bytesDownloaded += length; setChanged(); notifyObservers(getBytesDownloaded()); } if (isCancelled) { this.updateState(DownloadState.CANCELED); logger.info("The download has been cancelled."); } else { // The download was not cancelled. out.flush(); if (lastModified > 0) { file.setLastModified(lastModified); } if (bytesDownloaded != totalBytes) { logger.warn("The expected bytes to download is {}, but got {}.", getTotalBytes(), getBytesDownloaded()); this.updateState(DownloadState.ERROR); } this.updateState(DownloadState.FINISHED); logger.info("Download of '{}' finished successfully.", url); } // If the file did not exist before the download but does exit now, // we must remove it if an error occurred or if the download was // cancelled. if (getState() == DownloadState.CANCELED || getState() == DownloadState.ERROR) { if (!fileExisted && file.exists()) { FileUtils.forceDelete(file); } } } catch (HttpException he) { this.updateState(DownloadState.ERROR); logger.error("Fatal protocol violation : " + he); throw new HttpException("Error while downloading from the url '" + url + "'", he); } catch (IOException ioe) { this.updateState(DownloadState.ERROR); logger.error("Fatal transport error : " + ioe); throw new IOException(ioe); } finally { if (method != null) method.releaseConnection(); if (responseBody != null) responseBody.close(); if (out != null) out.close(); } }