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

StringdownloadAsString(String url)
download As String
try (InputStream is = new URL(url).openStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr)) {
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line).append('\n');
    return sb.toString();
voiddownloadBinary(URL BaseURL, String Name, File TargetDirectory)
download Binary
URL TheURL = new URL(BaseURL, Name);
File TestFile = new File(TargetDirectory, Name);
downloadBinary(TheURL, TestFile);
byte[]downloadData(String url, String params)
download Data
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream output = connection.getOutputStream();
output.write(params.getBytes());
output.close();
int code = connection.getResponseCode();
...
voiddownloadDirectory(URL dirUrl, File destDir)
Downloads url directory to target directory (url directory will be subdirectory of target directory); example: dowloadDirectory(new URL("http://dir0/dir1/dir2"), new File("/home/junk")) behaves like cp -r dir2 /home/junk/dir2 such that the directory /home/junk/dir2 exists with all its sub dirs and files.
if (!destDir.exists())
    throw new IOException(destDir + " does not exist.");
if (!destDir.isDirectory())
    throw new IOException(destDir + " is not a directory.");
Vector<String> dirListing = getFileContents(dirUrl);
String urlDirName = (new File(dirUrl.getFile()).getName());
File destDir2 = new File(destDir, urlDirName);
for (String line : dirListing) {
...
FiledownloadFile(File parent, String prefix, String suffix, URL url)
Download file.
File file = File.createTempFile(prefix, suffix, parent);
URLConnection conn = null;
final String protocol = url.getProtocol().toLowerCase();
try {
    conn = url.openConnection();
    if ("http".equals(protocol) || "https".equals(protocol)) {
        HttpURLConnection http = (HttpURLConnection) conn;
        http.setInstanceFollowRedirects(false);
...
booleandownloadFile(File target, String urlStr)
download File
InputStream in = openStream(urlStr);
if (in != null) {
    System.out.println("downloading " + urlStr + "...");
    writeStreamToFile(target, in);
    System.out.println("   ...done");
} else {
    System.out.println("no file found at " + urlStr);
return in != null;
byte[]downloadFile(final String url)
download File
final URL fileUrl = new URL(url);
final InputStream in = fileUrl.openStream();
final ByteArrayOutputStream buf = new ByteArrayOutputStream();
final byte[] readBuf = new byte[1024];
int read = in.read(readBuf);
while (read > 0) {
    buf.write(readBuf, 0, read);
    read = in.read(readBuf);
...
voiddownloadFile(String fileURL, String localFileName, String destinationDir)
download File
OutputStream outStream = null;
InputStream is = null;
try {
    URL Url = new URL(fileURL);
    outStream = new BufferedOutputStream(new FileOutputStream(destinationDir + '/' + localFileName));
    URLConnection uCon = Url.openConnection();
    is = uCon.getInputStream();
    int byteRead = 0;
...
voiddownloadFile(String fileURL, String saveDir)
Downloads a file from a URL
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
    String fileName = "";
    String disposition = httpConn.getHeaderField("Content-Disposition");
    String contentType = httpConn.getContentType();
    int contentLength = httpConn.getContentLength();
...
StringdownloadFile(String fileURL, String savePath)
Downloads a file from a URL
System.err.printf("Downloading %s to %s\n", fileURL, savePath);
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
    String fileName = "";
    String disposition = httpConn.getHeaderField("Content-Disposition");
    String contentType = httpConn.getContentType();
...