Example usage for org.apache.commons.io FileUtils copyURLToFile

List of usage examples for org.apache.commons.io FileUtils copyURLToFile

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils copyURLToFile.

Prototype

public static void copyURLToFile(URL source, File destination) throws IOException 

Source Link

Document

Copies bytes from the URL source to a file destination.

Usage

From source file:org.artifactory.common.ArtifactoryHome.java

public void writeBundledHaArtifactoryProperties() {
    File artifactoryHaPropertiesFile = getHaArtifactoryPropertiesFile();
    //Copy the artifactory.properties file into the data folder
    try {/*from  www  .  ja  v a  2 s.  c o  m*/
        //Copy from default
        FileUtils.copyURLToFile(getDefaultArtifactoryPropertiesUrl(), artifactoryHaPropertiesFile);
    } catch (IOException e) {
        throw new RuntimeException("Could not copy " + ARTIFACTORY_PROPERTIES_FILE + " to "
                + artifactoryHaPropertiesFile.getAbsolutePath(), e);
    }
}

From source file:org.artifactory.common.ArtifactoryHome.java

/**
 * Copy the system properties file and set its data as system properties
 *//*from   ww w .  j a v a  2  s . c o m*/
public void initAndLoadSystemPropertyFile() {
    // Expose the properties inside artifactory.system.properties
    File systemPropertiesFile = getArtifactorySystemPropertiesFile();
    if (!systemPropertiesFile.exists()) {
        try {
            //Copy from default
            URL url = ArtifactoryHome.class
                    .getResource("/META-INF/default/" + ARTIFACTORY_SYSTEM_PROPERTIES_FILE);
            if (url == null) {
                throw new RuntimeException("Could not read classpath resource '/META-INF/default/"
                        + ARTIFACTORY_SYSTEM_PROPERTIES_FILE
                        + "'. Make sure Artifactory home is readable by the current user.");
            }
            FileUtils.copyURLToFile(url, systemPropertiesFile);
        } catch (IOException e) {
            throw new RuntimeException("Could not create the default '" + ARTIFACTORY_SYSTEM_PROPERTIES_FILE
                    + "' at '" + systemPropertiesFile.getAbsolutePath() + "'.", e);
        }
    }
    artifactorySystemProperties = new ArtifactorySystemProperties();
    artifactorySystemProperties.loadArtifactorySystemProperties(systemPropertiesFile,
            getHomeArtifactoryPropertiesFile());
}

From source file:org.artifactory.common.ArtifactoryHome.java

public void initAndLoadMimeTypes() {
    File mimeTypesFile = getHaAwareMimeTypesFile();
    if (!mimeTypesFile.exists()) {
        // Copy default mime types configuration file
        try {//  w  w w  . java  2 s  .c om
            URL configUrl = ArtifactoryHome.class
                    .getResource("/META-INF/default/" + ArtifactoryHome.MIME_TYPES_FILE_NAME);
            FileUtils.copyURLToFile(configUrl, mimeTypesFile);
        } catch (Exception e) {
            throw new IllegalStateException("Couldn't start Artifactory. "
                    + "Failed to copy default mime types file: " + mimeTypesFile.getAbsolutePath(), e);
        }
    }

    try {
        String mimeTypesXml = Files.toString(mimeTypesFile, Charsets.UTF_8);
        mimeTypes = new MimeTypesReader().read(mimeTypesXml);
    } catch (Exception e) {
        throw new RuntimeException("Failed to parse mime types file from: " + mimeTypesFile.getAbsolutePath(),
                e);
    }
}

From source file:org.atomserver.core.validators.XSDValidator.java

public void setNamespaceMappings(Properties properties)
        throws SAXNotSupportedException, SAXNotRecognizedException, IOException {
    parser = new DOMParser();
    parser.setFeature("http://xml.org/sax/features/validation", true);
    Enumeration<?> uris = properties.propertyNames();
    while (uris.hasMoreElements()) {
        String uri = (String) uris.nextElement();
        String xsdLocation = properties.getProperty(uri);
        File tmpFile = File.createTempFile("temp", ".xsd");
        InputStream s = getClass().getClassLoader().getResourceAsStream(xsdLocation);
        if (s != null) {
            IOUtils.copy(s, new FileWriter(tmpFile));
            log.debug("found " + xsdLocation + " as a classpath resource");
        } else {/*from   w ww  .j av a2 s .  co  m*/
            File fileLoc = new File(xsdLocation);
            if (fileLoc.exists() && fileLoc.isFile()) {
                FileUtils.copyFile(fileLoc, tmpFile);
                log.debug("found " + xsdLocation + " as a file system resource");
            } else {
                try {
                    URL urlLoc = new URL(xsdLocation);
                    FileUtils.copyURLToFile(urlLoc, tmpFile);
                    log.debug("found " + xsdLocation + " as a url resource");
                } catch (MalformedURLException e) {
                    throw new IllegalArgumentException("could not find XSD resource:" + xsdLocation);
                }
            }
        }

        parser.setProperty(uri, tmpFile.getAbsolutePath());
    }
    parser.setErrorHandler(this);
}

From source file:org.b3log.latke.Latkes.java

/**
 * Gets a file in web application with the specified path.
 *
 * @param path the specified path/* w w w.  j  a  va2s  .  co  m*/
 * @return file,
 * @see ServletContext#getResource(String)
 * @see ServletContext#getResourceAsStream(String)
 */
public static File getWebFile(final String path) {
    final ServletContext servletContext = AbstractServletListener.getServletContext();

    File ret;

    try {
        final URL resource = servletContext.getResource(path);

        if (null == resource) {
            return null;
        }

        ret = FileUtils.toFile(resource);

        if (null == ret) {
            final File tempdir = (File) servletContext.getAttribute("javax.servlet.context.tempdir");

            ret = new File(tempdir.getPath() + path);

            FileUtils.copyURLToFile(resource, ret);

            ret.deleteOnExit();
        }

        return ret;
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Reads file [path=" + path + "] failed", e);

        return null;
    }
}

From source file:org.beangle.packagekit.wagon.HttpWagon.java

public void transfer(String from, String to) {
    try {/*from   ww  w. j  a  va  2 s  . c o m*/
        FileUtils.copyURLToFile(new URL(from), new File(to));
        logger.info("complete transfer package from {} to {}", from, to);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        throw new WagonException("failure transfer url" + from + " to " + to);
    }
}

From source file:org.betaconceptframework.astroboa.engine.jcr.io.ContentSourceExtractor.java

public InputStream extractStream(URI contentSource) throws Exception {
    dispose();//from  ww  w .j  a  v a 2  s  . c om

    String filename = contentSource.toURL().getFile();

    if (StringUtils.isBlank(filename)) {
        throw new Exception("No file name from URL " + contentSource.toString());
    }

    if (filename.endsWith(".zip")) {
        tmpZip = File.createTempFile("rep", ".zip");

        FileUtils.copyURLToFile(contentSource.toURL(), tmpZip);

        zipFile = new ZipFile(tmpZip);

        Enumeration entries = zipFile.getEntries();

        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = (ZipArchiveEntry) entries.nextElement();

            if (entry.getName() != null && entry.getName().endsWith(".xml")) {
                return zipFile.getInputStream(entry);
            }
        }

        return null;
    } else if (filename.endsWith(".xml")) {
        return contentSource.toURL().openStream();
    } else {
        throw new Exception("Unsupported file extension " + filename);
    }
}

From source file:org.bonitasoft.engine.bdm.client.ResourcesLoader.java

private void addJavaFileToDirectory(URL javaFile, String originalPackage, File destDirectory)
        throws IOException {
    File packageDirectory = createPackageDirectory(javaFile, originalPackage, destDirectory);
    File destinationFile = new File(packageDirectory, getName(javaFile.toString()));
    FileUtils.copyURLToFile(javaFile, destinationFile);
}

From source file:org.bonitasoft.engine.business.data.generator.client.ResourcesLoader.java

private static void addJavaFileToDirectory(URL javaFile, String originalPackage, File destDirectory)
        throws IOException {
    File packageDirectory = createPackageDirectory(javaFile, originalPackage, destDirectory);
    File destinationFile = new File(packageDirectory, getName(javaFile.toString()));
    FileUtils.copyURLToFile(javaFile, destinationFile);
}

From source file:org.canova.cli.subcommands.TestVectorize.java

/**
 * Added another copy of this method because MNISTFetcher uses the user's system home directory
 * and we couldnt specify that in the canova conf file
 *
 * @return// w  ww.  j  a  v a2s  .  c  o  m
 * @throws IOException
 */
public static File downloadAndUntar() throws IOException {

    File fileDir;

    //      if(fileDir != null) {
    //      return fileDir;
    //}
    // mac gives unique tmp each run and we want to store this persist
    // this data across restarts
    //File tmpDir = new File(System.getProperty("user.home"));

    File baseDir = new File(MNIST_ROOT);
    if (!(baseDir.isDirectory() || baseDir.mkdir())) {
        throw new IOException("Could not mkdir " + baseDir);
    }

    //log.info("Downloading mnist...");
    // getFromOrigin training records
    File tarFile = new File(baseDir, trainingFilesFilename);

    if (!tarFile.isFile()) {
        FileUtils.copyURLToFile(new URL(trainingFilesURL), tarFile);
    }

    ArchiveUtils.unzipFileTo(tarFile.getAbsolutePath(), baseDir.getAbsolutePath());

    // getFromOrigin training records
    File labels = new File(baseDir, trainingFileLabelsFilename);

    if (!labels.isFile()) {
        FileUtils.copyURLToFile(new URL(trainingFileLabelsURL), labels);
    }

    ArchiveUtils.unzipFileTo(labels.getAbsolutePath(), baseDir.getAbsolutePath());

    fileDir = baseDir;
    return fileDir;
}