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

FiledownloadFile(String fileURL, String targetDirectory)
download File
System.out.println("Retrieving: " + fileURL);
String cacheDir = System.getProperty("cacheDirectory");
if (cacheDir == null) {
    cacheDir = System.getProperty("java.io.tmpdir") + File.separator + "pdt-test-cache";
new File(cacheDir).mkdirs();
URL url = new URL(fileURL);
String fileName = url.getFile().substring(url.getPath().lastIndexOf('/') + 1);
...
byte[]downloadFile(String url)
download File
try {
    ArrayList<Byte> array = new ArrayList<Byte>(); 
    byte aByte; 
    Byte abyte; 
    URL yahoo = new URL(url);
    URLConnection fileLocation = yahoo.openConnection();
    BufferedInputStream bis = new BufferedInputStream(fileLocation.getInputStream());
    byte[] buff = new byte[2048];
...
voiddownloadFile(String url, File output)
download File
try {
    URLConnection con = createConnection(new URL(url));
    try (FileOutputStream fos = new FileOutputStream(output); InputStream in = con.getInputStream()) {
        byte[] buffer = new byte[1024];
        for (int i; (i = in.read(buffer)) != -1;) {
            fos.write(buffer, 0, i);
} catch (Exception ex) {
    System.out.println("Error while downloading file: " + ex.getMessage());
voiddownloadFile(String url, String fileName)
download File
try {
    URLConnection urlCon = new URL(url).openConnection();
    InputStream is = urlCon.getInputStream();
    FileOutputStream fos = new FileOutputStream(fileName);
    byte[] buffer = new byte[1000];
    int bytesRead = is.read(buffer);
    while (bytesRead > 0) {
        fos.write(buffer, 0, bytesRead);
...
voiddownloadFile(String url, String fileName)
download File
byte[] buf = new byte[1024];
FileOutputStream fos = new FileOutputStream(fileName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
InputStream in = new URL(url).openStream();
try {
    int numRead = in.read(buf);
    while (numRead != -1) {
        bos.write(buf, 0, numRead);
...
booleandownloadFile(String url, String fileName)
Downloads the file specified by the URL.
URL u = new URL(url);
try (final InputStream is = new BufferedInputStream(u.openStream())) {
    try (final OutputStream os = new FileOutputStream(fileName)) {
        byte[] b = new byte[1024];
        int len;
        while ((len = is.read(b, 0, b.length)) != -1) {
            os.write(b, 0, len);
} catch (MalformedURLException e) {
    throw new MalformedURLException("Invalid URL " + url);
} catch (IOException e) {
    throw new IOException("Error trying to access the file " + fileName, e);
return true;
FiledownloadFile(String url, String filePath, File parent)
Download a provided file
File file = null;
if (url != null && filePath != null) {
    if (parent != null) {
        file = new File(parent.getAbsolutePath() + filePath);
    } else {
        file = new File(filePath);
    if (!file.exists()) {
...
voiddownloadFile(String url, String path)
download File
BufferedInputStream in = null;
FileOutputStream fout = null;
try {
    in = new BufferedInputStream(new URL(url).openStream());
    fout = new FileOutputStream(path);
    final byte data[] = new byte[1024];
    int count;
    while ((count = in.read(data, 0, 1024)) != -1) {
...
voiddownloadFile(String urlPath, File file)
Download a remote http file
URL url = new URL(urlPath);
URLConnection conn = url.openConnection();
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));) {
    String inputLine;
    while ((inputLine = br.readLine()) != null) {
        bw.write(inputLine);
StringdownloadFile(String urlString)
Downloads the contents from the given urlString, and stores it in a temporary directory.
System.out.println("Downloading contents from url: " + urlString);
URL url = new URL(urlString);
File file = File.createTempFile("promise_pattern", null);
try (Reader reader = new InputStreamReader(url.openStream());
        BufferedReader bufferedReader = new BufferedReader(reader);
        FileWriter writer = new FileWriter(file)) {
    for (String line; (line = bufferedReader.readLine()) != null;) {
        writer.write(line);
...