Java URL Copy copyUrlBinaryFile(String fileName, String sourceUrlPath, String destPath)

Here you can find the source of copyUrlBinaryFile(String fileName, String sourceUrlPath, String destPath)

Description

This method will open any URL input source and copy the content to local path as binary file

License

Open Source License

Parameter

Parameter Description
fileName a parameter
sourceUrlPath a parameter
destPath a parameter

Declaration

public static void copyUrlBinaryFile(String fileName, String sourceUrlPath, String destPath) 

Method Source Code

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

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;

import java.net.URL;

public class Main {
    /**/*from ww  w . ja va 2 s  .c  o m*/
     * This method will open any URL input source and copy the content to local
     * path as binary file
     * 
     * @param fileName
     * @param sourceUrlPath
     * @param destPath
     */
    public static void copyUrlBinaryFile(String fileName, String sourceUrlPath, String destPath) {
        try {

            // Check whether the file is already there locally
            File temp = new File(destPath + fileName);
            if (temp.exists())
                return;

            URL url = new URL(sourceUrlPath + fileName);
            BufferedInputStream bis = new BufferedInputStream(url.openStream(), 10240);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath + fileName), 10240);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = bis.read(buffer)) != -1) {
                if (bytesRead > 0)
                    bos.write(buffer, 0, bytesRead);
            }
            bos.close();
            bis.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Related

  1. copyFile(URL url, String destFilePath)
  2. copyFromURL(final String query, final File target)
  3. copyRemoteFile(URL url, File localFile)
  4. copyResource(URL from, File to)
  5. copyResource(URL resource, File destination)
  6. copyURLToFile(final URL source, final File destination)
  7. copyURLToFile(URL source, File destination)