Java URL Download downloadToDirectory(URL url, String directoryName)

Here you can find the source of downloadToDirectory(URL url, String directoryName)

Description

Utility to download a remote file from a URL and place the contents into a directory.

License

Open Source License

Parameter

Parameter Description
url The URL of the remote file
directoryName The name of the directory into which to place the downloaded file

Exception

Parameter Description
IOException When an error occurs either reading the remote file or writing it locally

Declaration

public static String downloadToDirectory(URL url, String directoryName) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w w w. jav a 2 s.co  m
 * Copyright 2004 by EkoLiving Pty Ltd.  All Rights Reserved.
 *
 * This software is the proprietary information of EkoLiving Pty Ltd.
 * Use is subject to license terms.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

public class Main {
    private static final int BUFFER_SIZE = 2 ^ 14;

    /**
     * Utility to download a remote file from a URL and place the
     * contents into a directory. A URL takes the form
     * <code>protocol://host/full/path/to/file?arguments</code>. This method
     * will create a file in the specified directory with a name generated from
     * the last portion of the path. For example if you specified a URL of
     * <code>http://www.server.com/documents/new/test.html</code> the filename
     * used will be <code>test.html</code>
     * 
     * @param url  The URL of the remote file
     * @param directoryName The name of the directory into which to place the downloaded file
     * @throws IOException When an error occurs either reading the remote file or writing it locally
     */
    public static String downloadToDirectory(URL url, String directoryName) throws IOException {
        BufferedInputStream inputStream = new BufferedInputStream(url.openStream());
        String pathname = url.getPath();
        String filename = pathname;
        int index = pathname.lastIndexOf("/");
        if (index != -1) {
            filename = pathname.substring(index + 1);
        }

        int bytes;
        byte[] buffer = new byte[BUFFER_SIZE];

        String destinationFilename = directoryName + File.separator + filename;
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(destinationFilename));
        while ((bytes = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytes);
        }
        outputStream.close();
        inputStream.close();

        return destinationFilename;
    }
}

Related

  1. downloadString(URL url)
  2. downloadString(URL url)
  3. downloadText(String url)
  4. downloadTextFromUrl(String url)
  5. downloadTo(String url, String filename)
  6. downloadToFile(String url, File file)
  7. downloadToFile(URL source, File dest)
  8. downloadToFile(URL url, File downloadFile)
  9. downloadToFile(URL url, File file)