Java Reference
In-Depth Information
public class DownloadURL
{
public static int BUFFER_SIZE = 8192;
/**
* Download either a text or binary file from a URL.
* The URL's headers will be scanned to determine the
* type of tile.
*
* @param remoteURL The URL to download from.
* @param localFile The local file to save to.
* @throws IOException Exception while downloading.
*/
public void download(URL remoteURL, File localFile)
throws IOException
{
HttpURLConnection http =
(HttpURLConnection) remoteURL.openConnection();
HttpURLConnection.setFollowRedirects(true);
InputStream is = http.getInputStream();
OutputStream os = new FileOutputStream(localFile);
String type =
http.getHeaderField("Content-Type").toLowerCase().trim();
if (type.startsWith("text"))
downloadText(is, os);
else
downloadBinary(is, os);
is.close();
os.close();
http.disconnect();
}
/**
* Overloaded version of download that accepts strings,
* rather than URL objects.
*
* @param remoteURL The URL to download from.
* @param localFile The local file to save to.
* @throws IOException Exception while downloading.
*/
public void download(String remoteURL, String localFile)
throws IOException
{
Search WWH ::




Custom Search