Java Utililty Methods URL Download

List of utility methods to do URL Download

Description

The list of methods to do URL Download are organized into topic(s).

Method

booleandownloadToFile(String url, File file)
Downloads from a given URL to a given File
BufferedInputStream in = null;
FileOutputStream fout = null;
try {
    in = new BufferedInputStream(new URL(url).openStream());
    fout = new FileOutputStream(file);
    final byte data[] = new byte[1024];
    int count;
    while ((count = in.read(data, 0, 1024)) != -1) {
...
voiddownloadToFile(URL source, File dest)
Retrieves a file from a web server and writes it to disk.
final InputStream input = source.openStream();
byte[] buffer = new byte[4096];
int n = -1;
final OutputStream output = new FileOutputStream(dest);
while ((n = input.read(buffer)) != -1) {
    output.write(buffer, 0, n);
input.close();
...
voiddownloadToFile(URL url, File downloadFile)
download To File
long startTime = System.currentTimeMillis();
System.out.println(String.format("Begin download from %s...\n", url));
url.openConnection();
InputStream reader = url.openStream();
File parentFile = downloadFile.getParentFile();
if (!parentFile.exists())
    parentFile.mkdirs();
FileOutputStream writer = new FileOutputStream(downloadFile);
...
voiddownloadToFile(URL url, File file)
Download a file from a URL.
InputStream is = null;
OutputStream os = null;
try {
    URLConnection conn = url.openConnection();
    conn.setUseCaches(false);
    byte[] buffer = new byte[2048];
    is = conn.getInputStream();
    os = new FileOutputStream(file);
...
voiddownloadToLocalFile(File targetFile, URL requestedURL, Proxy proxy)
download To Local File
InputStream is = open(requestedURL, proxy);
targetFile.createNewFile();
FileOutputStream out = new FileOutputStream(targetFile);
byte[] buf = new byte[1024]; 
int bytesRead;
while ((bytesRead = is.read(buf)) != -1) {
    out.write(buf, 0, bytesRead);
close(is);
out.close();
StringdownloadToString(String url)
download To String
URL website = null;
try {
    website = new URL(url);
} catch (MalformedURLException e) {
    System.out.println("Couldn't connect to " + url);
    return null;
URLConnection connection = null;
...
voiddownloadURL(String addr, File outFile)
download URL
downloadURL(new URL(addr), outFile);
voiddownloadURL(String url, String outFile)
download URL
write((new URL(url)).openStream(), outFile);
voiddownloadURL(URL url, File file)
Download the contents of the given URL to the given file
URLConnection conn = url.openConnection();
InputStream stream = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int read = 0;
while ((read = stream.read(buffer)) != -1) {
    fos.write(buffer, 0, read);
StringdownloadURL(URL url, String localPath)
download URL
InputStream reader = url.openStream();
String[] path = url.getPath().split("/");
String filename = path[path.length - 1];
File fullpath = new File(localPath, filename);
FileOutputStream writer = new FileOutputStream(fullpath);
byte[] buffer = new byte[DOWNLOAD_BUFFER_SIZE];
int totalBytesRead = 0;
int bytesRead = 0;
...