Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
jerry-http - Common Java Functionality Copyright (c) 2012-2014, Sandeep Gupta http://sangupta.com/projects/jerry-http Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package com.sangupta.jerry.util;
 
 import java.io.File;
 
 
Function to handle file downloads from the web via HTTP. More schemes may be added in future.

Author(s):
sangupta
 
 public class WebUtils {

My logger instance
 
 	private static final Logger logger = LoggerFactory.getLogger(WebUtils.class);

Download the file at the given location URL and store it as a temporary file on disk. The temporary file is set to be deleted at the exit of the application.

Parameters:
url absolute URL of the file
Returns:
java.io.File handle of the temporary file that was written to disk if successful, null otherwise.
Throws:
java.io.IOException in case something fails
 
 	public static File downloadToTempFile(String urlthrows IOException {
 		String extension = UriUtils.extractExtension(url);
 		File tempFile = File.createTempFile("download"extension);
 		tempFile.deleteOnExit();
 		
 		.debug("Downloading {} to {}"urltempFile.getAbsolutePath());
 		
 		try {
 			WebRequest.get(url).execute().writeToFile(tempFile);
 			return tempFile;
 		} catch(HttpResponseException e) {
 			.error("HTTP response did not yield an OK status"e);
 		} catch(IOException e) {
 			.error("Unable to download url to temp file"e);
 		}
 		
 		return null;
 	}

Download the file at the given location URL and store it in the file mentioned on disk. If the file exists, it will be over-written.

Parameters:
url absolute URL of the file
fileToDownloadIn java.io.File in which contents are written
Returns:
true if file was successfully downloaded, false otherwise.
Throws:
java.io.IOException in case something fails
 
 	public static boolean downloadToFile(String urlFile fileToDownloadInthrows IOException {
 		try {
 			WebRequest.get(url).execute().writeToFile(fileToDownloadIn);
 			return true;
catch(HttpResponseException e) {
			.error("HTTP response did not yield an OK status"e);
catch(IOException e) {
			.error("Unable to download url to temp file"e);
		}
		return false;
	}
New to GrepCode? Check out our FAQ X