Java URL Download downloadFile(String url, String fileName)

Here you can find the source of downloadFile(String url, String fileName)

Description

download File

License

Open Source License

Declaration

public static void downloadFile(String url, String fileName) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.net.*;

public class Main {
    public static void downloadFile(String url, String fileName) {
        try {/*from  www. j  av a 2s . c o  m*/
            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);
                bytesRead = is.read(buffer);
            }
            is.close();
            fos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Related

  1. downloadFile(String fileURL, String targetDirectory)
  2. downloadFile(String url)
  3. downloadFile(String url, File output)
  4. downloadFile(String url, String fileName)
  5. downloadFile(String url, String fileName)
  6. downloadFile(String url, String filePath, File parent)
  7. downloadFile(String url, String path)
  8. downloadFile(String urlPath, File file)
  9. downloadFile(String urlString)