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:com.intuit.cto.selfservice.service.Util.java

/**
 * Extract files from a package on the classpath into a directory.
 * @param packagePath e.g. "com/stuff" (always forward slash not backslash, never dot)
 * @param toDir directory to extract to/* w  w w .java  2 s . c  o  m*/
 * @return int the number of files copied
 * @throws java.io.IOException if something goes wrong, including if nothing was found on classpath
 */
public static int extractFromClasspathToFile(String packagePath, File toDir) throws IOException {
    String locationPattern = "classpath*:" + packagePath + "/**";
    ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    Resource[] resources = resourcePatternResolver.getResources(locationPattern);
    if (resources.length == 0) {
        throw new IOException("Nothing found at " + locationPattern);
    }
    int counter = 0;
    for (Resource resource : resources) {
        if (resource.isReadable()) { // Skip hidden or system files
            URL url = resource.getURL();
            String path = url.toString();
            if (!path.endsWith("/")) { // Skip directories
                int p = path.lastIndexOf(packagePath) + packagePath.length();
                path = path.substring(p);
                File targetFile = new File(toDir, path);
                long len = resource.contentLength();
                if (!targetFile.exists() || targetFile.length() != len) { // Only copy new files
                    FileUtils.copyURLToFile(url, targetFile);
                    counter++;
                }
            }
        }
    }
    logger.info("Unpacked {} files from {} to {}", new Object[] { counter, locationPattern, toDir });
    return counter;
}

From source file:Fasta.java

public static String Download(String name) {
    String path;//  w w  w  .  jav  a 2s .  c o  m
    if (name.equals("BrownCNA")) {
        path = "http://phagesdb.org/media/fastas/Browncna.fasta";
    } else if (name.equals("GUmbie")) {
        path = "http://phagesdb.org/media/fastas/Gumbie.fasta";
    } else if (name.equals("Numberten")) {
        path = "http://phagesdb.org/media/fastas/NumberTen.fasta";
    } else if (name.equals("Seabiscuit")) {
        path = "http://phagesdb.org/media/fastas/SeaBiscuit.fasta";
    } else if (name.equals("Caliburn")) {
        path = "http://phagesdb.org/media/fastas/Excalibur.fasta";
    } else if (name.equals("Godpower")) {
        path = "http://phagesdb.org/media/fastas/GodPower.fasta";
    } else if (name.equals("Romney")) {
        path = "http://phagesdb.org/media/fastas/Romney2012.fasta";
    } else {
        path = "http://phagesdb.org/media/fastas/" + name + ".fasta";
    }
    String base = new File("").getAbsolutePath();
    name = base + "/Fastas/" + name + ".fasta";
    File file = new File(name);
    try {
        if (!file.exists()) {
            URL netPath = new URL(path);
            FileUtils.copyURLToFile(netPath, file);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file.toString();
}

From source file:com.teambr.modularsystems.core.registries.BlockValueRegistry.java

/**
  * Used to generate the default values/*from w ww  .ja  v a 2  s.co m*/
  */
public void generateDefaults() {
    validateList();
    //Move file and load it
    File file = new File(ModularSystems.configFolderLocation() + File.separator + "Registries" + File.separator
            + "blockValues.json");
    if (!file.exists()) {
        URL fileURL = ModularSystems.class.getResource("/blockValues.json");
        try {
            FileUtils.copyURLToFile(fileURL, file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    values = JsonUtils.<LinkedHashMap<String, BlockValues>>readFromJson(
            new TypeToken<LinkedHashMap<String, BlockValues>>() {
            }, ModularSystems.configFolderLocation() + File.separator + "Registries" + File.separator
                    + "blockValues.json");

    addMaterialValues(Material.ROCK, new Calculation(-1, 200, 0, 1, 0, -100, 0),
            new Calculation(1, 100, 0, 1, 0, 0, 450), new Calculation(1, 1, 0, 1, 0, 0, 0));
    addMaterialValues(Material.IRON, new Calculation(1, 100, 0, 1, 0, 100, 0),
            new Calculation(10, 1, 0, 1, 0, 0, 1600), new Calculation(1, 1, 0, 1, 0, 0, 0));
}

From source file:de.shadowhunt.subversion.internal.AbstractPrepare.java

public void pullCurrentDumpData() throws Exception {
    final File zip = new File(base, "dump.zip");
    if (zip.exists()) {
        final String localMD5 = calculateMd5(zip);
        final String remoteMD5 = copyUrlToString(md5Uri.toURL());
        if (localMD5.equals(remoteMD5)) {
            return;
        }/*w  w w. j a v  a  2 s.c  om*/
    }
    FileUtils.deleteQuietly(base);

    base.mkdirs();
    FileUtils.copyURLToFile(dumpUri.toURL(), zip);
    extractArchive(zip, base);
}

From source file:azkaban.restli.ProjectManagerResource.java

@Action(name = "deploy")
public String deploy(@ActionParam("sessionId") String sessionId, @ActionParam("projectName") String projectName,
        @ActionParam("packageUrl") String packageUrl)
        throws ProjectManagerException, UserManagerException, ServletException, IOException {
    logger.info("Deploy called. {sessionId: " + sessionId + ", projectName: " + projectName + ", packageUrl:"
            + packageUrl + "}");

    String ip = (String) this.getContext().getRawRequestContext().getLocalAttr("REMOTE_ADDR");
    User user = ResourceUtils.getUserFromSessionId(sessionId, ip);
    ProjectManager projectManager = getAzkaban().getProjectManager();
    Project project = projectManager.getProject(projectName);
    if (project == null) {
        throw new ProjectManagerException("Project '" + projectName + "' not found.");
    }/*from w  ww . j ava2s.  c  o m*/

    if (!ResourceUtils.hasPermission(project, user, Permission.Type.WRITE)) {
        String errorMsg = "User " + user.getUserId() + " has no permission to write to project "
                + project.getName();
        logger.error(errorMsg);
        throw new ProjectManagerException(errorMsg);
    }

    logger.info("Target package URL is " + packageUrl);
    URL url = null;
    try {
        url = new URL(packageUrl);
    } catch (MalformedURLException e) {
        String errorMsg = "URL " + packageUrl + " is malformed.";
        logger.error(errorMsg, e);
        throw new ProjectManagerException(errorMsg, e);
    }

    String filename = getFileName(url.getFile());
    File tempDir = Utils.createTempDir();
    File archiveFile = new File(tempDir, filename);
    try {
        // Since zip files can be large, don't specify an explicit read or
        // connection
        // timeout. This will cause the call to block until the download is
        // complete.
        logger.info("Downloading package from " + packageUrl);
        FileUtils.copyURLToFile(url, archiveFile);
        Props props = new Props();

        logger.info("Downloaded to " + archiveFile.toString());
        projectManager.uploadProject(project, archiveFile, "zip", user, props);
    } catch (IOException e) {
        String errorMsg = "Download of URL " + packageUrl + " to " + archiveFile.toString() + " failed";
        logger.error(errorMsg, e);
        throw new ProjectManagerException(errorMsg, e);
    } finally {
        if (tempDir.exists()) {
            FileUtils.deleteDirectory(tempDir);
        }
    }
    return Integer.toString(project.getVersion());
}

From source file:com.sonar.it.scanner.msbuild.CppTest.java

@Test
public void testCppOnly() throws Exception {
    String projectKey = "cpp";
    String fileKey = "cpp:cpp:A8B8B694-4489-4D82-B9A0-7B63BF0B8FCE:ConsoleApp.cpp";

    ORCHESTRATOR.getServer().restoreProfile(FileLocation.of("src/test/resources/TestQualityProfileCpp.xml"));
    ORCHESTRATOR.getServer().provisionProject(projectKey, "Cpp");
    ORCHESTRATOR.getServer().associateProjectToQualityProfile(projectKey, "cpp", "ProfileForTestCpp");

    Path projectDir = TestUtils.projectDir(temp, "CppSolution");
    File wrapperOutDir = new File(projectDir.toFile(), "out");

    ORCHESTRATOR.executeBuild(TestUtils.newScanner(ORCHESTRATOR, projectDir).addArgument("begin")
            .setProjectKey(projectKey).setProjectName("Cpp").setProjectVersion("1.0")
            .setProperty("sonar.cfamily.build-wrapper-output", wrapperOutDir.toString()));
    File buildWrapper = temp.newFile();
    File buildWrapperDir = temp.newFolder();
    FileUtils.copyURLToFile(
            new URL(ORCHESTRATOR.getServer().getUrl() + "/static/cpp/build-wrapper-win-x86.zip"), buildWrapper);
    ZipUtils.unzip(buildWrapper, buildWrapperDir);

    TestUtils.runMSBuildWithBuildWrapper(ORCHESTRATOR, projectDir,
            new File(buildWrapperDir, "build-wrapper-win-x86/build-wrapper-win-x86-64.exe"), wrapperOutDir,
            "/t:Rebuild");

    BuildResult result = ORCHESTRATOR//w ww  . ja  va 2  s.c  o m
            .executeBuild(TestUtils.newScanner(ORCHESTRATOR, projectDir).addArgument("end"));
    assertThat(result.getLogs()).doesNotContain("Invalid character encountered in file");

    List<Issue> issues = TestUtils.allIssues(ORCHESTRATOR);

    List<String> keys = issues.stream().map(i -> i.getRule()).collect(Collectors.toList());
    assertThat(keys).containsAll(Arrays.asList("cpp:S106"));

    assertThat(getMeasureAsInteger(projectKey, "ncloc")).isEqualTo(15);
    assertThat(getMeasureAsInteger(fileKey, "ncloc")).isEqualTo(8);
}

From source file:com.mcapanel.web.controllers.InstallController.java

@SuppressWarnings("unchecked")
public boolean process() throws IOException {
    if (isMethod("POST")) {
        includeIndex(false);//from   ww w .  j  a  v  a  2  s.c o m
        mimeType("application/json");

        JSONObject out = new JSONObject();

        if (!config.getBoolean("installed", false)) {
            String serverIp = request.getParameter("serverip");
            String webPort = request.getParameter("webport");

            String cbName = request.getParameter("cbname");
            String cbFile = request.getParameter("cbfile");
            String cbInstall = request.getParameter("cbinstall");

            String mcname = request.getParameter("mcname");
            String mcpass = request.getParameter("mcpass");
            String mcpassconf = request.getParameter("mcpassconf");

            String licemail = request.getParameter("licemail");
            String lickey = request.getParameter("lickey");

            if (mcpass.equals(mcpassconf)) {
                User u = new User(mcname, mcpass, RandomStringUtils.randomAlphanumeric(8),
                        request.getRemoteAddr().equals("0:0:0:0:0:0:0:1") ? "127.0.0.1"
                                : request.getRemoteAddr());

                u.setGroupId(db.find(Group.class).where().ieq("group_name", "Admin").findUnique().getId());
                u.setWhitelisted(true);

                db.save(u);

                loginUser(u);

                Server server = new Server(cbName, cbFile);
                db.save(server);

                BukkitServer bukkitServer = new BukkitServer(server);
                AdminPanelWrapper.getInstance().servers.put(server.getId(), bukkitServer);

                request.getSession().setAttribute("chosenServer", server.getId());
                bukkitServer.setupBackups();

                config.setValue("installed", "true");
                config.setValue("server-ip", serverIp);
                config.setValue("web-port", webPort);

                config.setValue("license-email", licemail);
                config.setValue("license-key", lickey);

                config.saveConfig();

                //ap.install();

                final BukkitVersion bv = BukkitVersion.getVersion(cbInstall);

                if (bv != null) {
                    new Thread(new Runnable() {
                        public void run() {
                            try {
                                System.out.println("Downloading CraftBukkit...");

                                File cbFile = new File("craftbukkit.jar");

                                FileUtils.copyURLToFile(new URL(bv.getUrl()), cbFile);

                                config.setValue("server-jar", cbFile.getAbsolutePath());
                                config.saveConfig();

                                //ap.install();

                                System.out.println("Done downloading CraftBukkit!");
                            } catch (MalformedURLException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }

                out.put("good",
                        "Successfully saved all installation settings.<br />It is recommended to create another user so you can see what they see.");
            } else
                out.put("error", "The passwords that you entered do not appear to match.");
        } else
            out.put("error", "You are not allowed to do that.");

        response.getWriter().println(out.toJSONString());

        return true;
    }

    return error();
}

From source file:ccm.pay2spawn.types.MusicType.java

@Override
public void printHelpList(File configFolder) {
    musicFolder = new File(configFolder, "music");
    if (musicFolder.mkdirs()) {
        new Thread(new Runnable() {
            @Override/* w w  w  .j a va  2  s .  co  m*/
            public void run() {
                try {
                    File zip = new File(musicFolder, "music.zip");
                    FileUtils.copyURLToFile(new URL(Constants.MUSICURL), zip);
                    ZipFile zipFile = new ZipFile(zip);
                    Enumeration<? extends ZipEntry> entries = zipFile.entries();
                    while (entries.hasMoreElements()) {
                        ZipEntry entry = entries.nextElement();
                        File entryDestination = new File(musicFolder, entry.getName());
                        entryDestination.getParentFile().mkdirs();
                        InputStream in = zipFile.getInputStream(entry);
                        OutputStream out = new FileOutputStream(entryDestination);
                        IOUtils.copy(in, out);
                        IOUtils.closeQuietly(in);
                        IOUtils.closeQuietly(out);
                    }
                    zipFile.close();
                    zip.delete();
                } catch (IOException e) {
                    Pay2Spawn.getLogger()
                            .warn("Error downloading music file. Get from github and unpack yourself please.");
                    e.printStackTrace();
                }
            }
        }, "Pay2Spawn music download and unzip").start();
    }
}

From source file:cz.cas.lib.proarc.common.process.GenericExternalProcessTest.java

/** onExit is an experimental feature .*/
@Test//from  w w  w .ja  v a2  s . c  o m
public void testIMConvertOnExit() throws Exception {
    String imageMagicExec = "/usr/bin/convert";
    Assume.assumeTrue(new File(imageMagicExec).exists());
    File confFile = temp.newFile("props.cfg");
    File root = temp.getRoot();
    URL pdfaResource = TiffImporterTest.class.getResource("pdfa_test.pdf");
    File pdfa = new File(root, "pdfa_test.pdf");
    FileUtils.copyURLToFile(pdfaResource, pdfa);
    FileUtils.writeLines(confFile,
            Arrays.asList("input.file.name=RESOLVED", "exec=" + imageMagicExec, "arg=-thumbnail", "arg=120x128",
                    "arg=$${input.file}[0]", "arg=-flatten", "arg=$${input.folder}/$${input.file.name}.jpg",
                    "result.file=$${input.folder}/$${input.file.name}.jpg", "onExits=0",
                    "onExit.0.param.output.file=$${input.folder}/$${input.file.name}.jpg", "id=test"));
    PropertiesConfiguration conf = new PropertiesConfiguration(confFile);
    GenericExternalProcess gep = new GenericExternalProcess(conf);
    gep.addInputFile(pdfa);
    gep.run();
    //        System.out.printf("#exit: %s, out: %s\nresults: %s\n",
    //                gep.getExitCode(), gep.getFullOutput(), gep.getResultParameters());
    assertEquals("exit code", 0, gep.getExitCode());
    File output = gep.getOutputFile();
    assertNotNull(output);
    assertTrue(output.toString(), output.exists());
    assertTrue("Not JPEG", InputUtils.isJpeg(output));
}

From source file:github.srlee309.lessWrongBookCreator.scraper.PostSectionExtractor.java

/**
 * @param src of image to download and save
 * @param folder to which to save the image
 * @param fileName to use for the saved image
 *///from  ww w . j  av a 2  s.  c o m
protected final void saveImage(String src, String folder, String fileName) {
    if (fileName.contains("?")) {
        fileName = fileName.substring(0, fileName.lastIndexOf("?"));
    }

    fileName = fileName.replaceAll("[^a-zA-Z0-9.-]", "_"); // replace non valid file fileName characters
    File outputFile = new File(folder + "\\" + fileName);
    try {
        URL url = new URL(src);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0");
        connection.setRequestMethod("HEAD");
        connection.setInstanceFollowRedirects(false);
        int rspCode = connection.getResponseCode();
        if (rspCode == 301) { // redirected, so get new url
            String newUrl = connection.getHeaderField("Location");
            url = new URL(newUrl);
        }
        connection.disconnect();
        FileUtils.copyURLToFile(url, outputFile);
    } catch (MalformedURLException e) {
        logger.error("Malformed url exception for image src:  " + src, e);
    } catch (IOException e) {
        logger.error("IO exception for saving image src locally:  " + src, e);
    }
}