Java URL Download downloadFileFromWeb(URL resourceURL, String fullPath)

Here you can find the source of downloadFileFromWeb(URL resourceURL, String fullPath)

Description

fullPath,eg: "D:\\ALL_integrate_patch_v4.2.2.r63143-20110722.zip

License

Open Source License

Declaration

public static void downloadFileFromWeb(URL resourceURL, String fullPath) 

Method Source Code


//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    private static final int BUFFER_SIZE = 64 * 1024;

    /** fullPath,eg: "D:\\ALL_integrate_patch_v4.2.2.r63143-20110722.zip **/
    public static void downloadFileFromWeb(URL resourceURL, String fullPath) {
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        HttpURLConnection conn = null;
        byte[] buf = new byte[BUFFER_SIZE];
        int size = 0;
        File fileTostore = null;/*from  w w  w  . j a  v  a2s  . co  m*/
        try {
            conn = (HttpURLConnection) resourceURL.openConnection();
            conn.connect();
            InputStream inputStream = conn.getInputStream();
            bis = new BufferedInputStream(inputStream);
            fileTostore = new File(fullPath);

            if (!fileTostore.exists()) {
                fileTostore.createNewFile();
            } else {
                fileTostore.delete();
                fileTostore.createNewFile();
            }
            fos = new FileOutputStream(fullPath);
            while ((size = bis.read(buf)) != -1) {
                fos.write(buf, 0, size);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                buf = null;
                bis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            conn.disconnect();
        }
    }
}

Related

  1. downloadFile(URL url, String name)
  2. downloadFile(URL url, String path, String fileName)
  3. downloadFileAndRetry(final File file, final URL url, final int retry)
  4. downloadFileFromInternet(String remoteUrl)
  5. downloadFileFromUrl(String urlPath, File file)
  6. downloadFileFromWebserver(String fileUrl, String storageLocation)
  7. downloadFileIfAvailable(URL url, File destination)
  8. downloadFileToGivenNameElseExtension( URLConnection urlConnection, String fileName)
  9. downloadFromURL(String url)