Here you can find the source of copyUrlBinaryFile(String fileName, String sourceUrlPath, String destPath)
Parameter | Description |
---|---|
fileName | a parameter |
sourceUrlPath | a parameter |
destPath | a parameter |
public static void copyUrlBinaryFile(String fileName, String sourceUrlPath, String destPath)
//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(); } } }