Java URL Download download(String fileUrl, String savePath, String fileName)

Here you can find the source of download(String fileUrl, String savePath, String fileName)

Description

download

License

Apache License

Declaration

public static void download(String fileUrl, String savePath, String fileName) 

Method Source Code


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

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {

    public static void download(String fileUrl, String savePath, String fileName) {
        OutputStream out = null;//from   w  w  w .  ja va2s  .co m
        DataInputStream input = null;
        try {
            File directory = new File(savePath);
            if (!directory.exists() && !directory.isDirectory()) {
                directory.mkdirs();
            }
            if (!checkFileExists(fileUrl)) {
                return;
            }
            URL url = new URL(fileUrl);
            input = new DataInputStream(url.openStream());
            File downFile = new File(savePath + fileName);
            if (downFile.exists() && downFile.isFile()) {
                downFile.delete();
            }
            out = new FileOutputStream(downFile);
            byte[] buffer = new byte[1024];
            int length = -1;
            while ((length = input.read(buffer)) != -1) {
                out.write(buffer, 0, length);
            }
            buffer = null;

        } catch (MalformedURLException e) {
        } catch (IOException e) {
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
            }

        }

    }

    public static boolean checkFileExists(String fileUrl) {
        boolean exist = false;

        try {
            URL url = new URL(fileUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            int code = connection.getResponseCode();
            if (code == 200) {
                exist = true;
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return exist;
    }
}

Related

  1. downImg(String filePath, String imageUrl, String fileName)
  2. download(final String sURL, final String sFilename)
  3. download(final URL url)
  4. download(String downloadUrlStr, String filename)
  5. download(String url)
  6. download(String url, String path)
  7. download(String url, String saveFile)
  8. download(String url, String toFile)