Java URL Load loadFile(URL url)

Here you can find the source of loadFile(URL url)

Description

load File

License

Open Source License

Declaration

public static File loadFile(URL url) throws IOException 

Method Source Code

//package com.java2s;
/*//from   w  ww . ja  va  2 s . c  o  m
 * Scriptographer
 *
 * This file is part of Scriptographer, a Scripting Plugin for Adobe Illustrator
 * http://scriptographer.org/
 *
 * Copyright (c) 2002-2010, Juerg Lehni
 * http://scratchdisk.com/
 *
 * All rights reserved. See LICENSE file for details.
 * 
 * File created on May 11, 2007.
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class Main {
    /**
     * Downloads a URL resource into a temporary file on the disk.
     * @param url
     * @throws IOException
     */
    public static File loadFile(URL url, String prefix) throws IOException {
        String name = url.getPath();
        int pos = name.lastIndexOf('.');
        if (pos != -1) {
            String ext = name.substring(pos);
            InputStream in = url.openStream();
            // Create temp file:
            File file = File.createTempFile(prefix, ext);
            // Delete temp file when program exits.
            file.deleteOnExit();
            // Write to temp file
            FileOutputStream out = new FileOutputStream(file);
            int bytesRead;
            byte[] buf = new byte[4 * 1024]; // 4K buffer
            while ((bytesRead = in.read(buf)) != -1) {
                out.write(buf, 0, bytesRead);
            }
            in.close();
            out.close();
            return file;
        }
        return null;
    }

    public static File loadFile(URL url) throws IOException {
        return loadFile(url, "");
    }
}

Related

  1. load(URL url)
  2. load(URL url, boolean allowCache)
  3. loadByteArray(URL url)
  4. loadFile(String url)
  5. loadFile(URL resource)
  6. loadFileNoArray(URL f)
  7. loadFileOrUrlToList(String iFileOrUrl)
  8. loadFromUrl(String url)
  9. loadFromURL(URL url)