Java URL Download downloadFile(String fileURL, String localFileName, String destinationDir)

Here you can find the source of downloadFile(String fileURL, String localFileName, String destinationDir)

Description

download File

License

Apache License

Declaration

public static void downloadFile(String fileURL, String localFileName, String destinationDir)
            throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.BufferedOutputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static void downloadFile(String fileURL, String localFileName, String destinationDir)
            throws IOException {
        OutputStream outStream = null;
        InputStream is = null;//from w w  w . jav  a 2 s. co m
        try {
            URL Url = new URL(fileURL);
            outStream = new BufferedOutputStream(new FileOutputStream(destinationDir + '/' + localFileName));
            URLConnection uCon = Url.openConnection();
            is = uCon.getInputStream();
            int byteRead = 0;
            byte[] buf = new byte[10000];
            while ((byteRead = is.read(buf)) != -1) {
                outStream.write(buf, 0, byteRead);
            }
        } finally {
            if (is != null) {
                is.close();
            }
            if (outStream != null) {
                outStream.close();
            }
            ;
        }
    }

    public static void downloadFile(String fileURL, String destinationDir) throws IOException {
        int slashIndex = fileURL.lastIndexOf('/');
        int periodIndex = fileURL.lastIndexOf('.');
        String fileName = fileURL.substring(slashIndex + 1);
        if (periodIndex >= 1 && slashIndex >= 0 && slashIndex < fileURL.length() - 1) {
            downloadFile(fileURL, fileName, destinationDir);
        } else {
            System.err.println("path or file name.");
        }
    }
}

Related

  1. downloadData(String url, String params)
  2. downloadDirectory(URL dirUrl, File destDir)
  3. downloadFile(File parent, String prefix, String suffix, URL url)
  4. downloadFile(File target, String urlStr)
  5. downloadFile(final String url)
  6. downloadFile(String fileURL, String saveDir)
  7. downloadFile(String fileURL, String savePath)
  8. downloadFile(String fileURL, String targetDirectory)
  9. downloadFile(String url)