Here you can find the source of copyRemoteFile(URL url, File localFile)
private static void copyRemoteFile(URL url, File localFile) throws IOException
//package com.java2s; /*/*from w ww . jav a 2s . c om*/ * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is * subject to license terms. * * This program is free software; you can redistribute it and/or modify * it under the terms of the Lesser GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */ import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.io.DataOutputStream; import java.io.DataInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; public class Main { private static void copyRemoteFile(URL url, File localFile) throws IOException { if (url == null || localFile == null) return; if (url.getFile() == "" || url.getFile() == null) return; if (localFile.isDirectory()) { localFile = new File(localFile.getPath() + File.separator + url.getFile()); } if (!localFile.exists()) { createLocalFile(localFile); } DataInputStream inStream = new DataInputStream(url.openStream()); DataOutputStream outStream = new DataOutputStream( new FileOutputStream(localFile)); copyStream(inStream, outStream); } private static void createLocalFile(File localFile) throws IOException { if (localFile == null) return; if (!localFile.getParentFile().exists()) { try { if (!localFile.getParentFile().mkdirs()) { throw new IOException("Cannot make parent directory " + "when trying to create local file"); } } catch (Exception e) { throw new IOException("Cannot make parent directory when " + "trying to create local file: " + e.getMessage()); } } if (localFile.exists()) { try { if (!localFile.delete()) { throw new IOException( "Cannot delete original file when " + "trying to create local file"); } } catch (Exception e) { throw new IOException("Cannot delete original file when " + "trying to create local file: " + e.getMessage()); } } try { if (!localFile.createNewFile()) { throw new IOException("Cannot create new local file"); } } catch (Exception e) { throw new IOException("Cannot create new local file: " + e.getMessage()); } } private static void copyStream(InputStream inStream, OutputStream outStream) throws IOException { int readbytes = 0; try { do { byte[] buffer = new byte[512]; readbytes = inStream.read(buffer, 0, 512); if (readbytes <= 0) { break; } outStream.write(buffer, 0, readbytes); outStream.flush(); } while (true); } catch (IOException ioE) { ioE.printStackTrace(); } finally { if (inStream != null) { inStream.close(); } if (outStream != null) { outStream.close(); } } } }