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.walmart.gatling.commons.ScriptExecutor.java

@Override
public void onReceive(Object message) {
    log.debug("Script worker received task: {}", message);
    if (message instanceof Master.Job) {
        Cancellable abortLoop = getContext().system().scheduler().schedule(Duration.Zero(),
                Duration.create(60, TimeUnit.SECONDS), () -> {
                    Master.Job job = (Master.Job) message;
                    runCancelJob(job);/* w w  w  .j av a  2 s. c o m*/
                }, getContext().system().dispatcher());
        ActorRef sender = getSender();
        ExecutorService pool = Executors.newFixedThreadPool(1);
        ExecutionContextExecutorService ctx = ExecutionContexts.fromExecutorService(pool);
        Future<Object> f = future(() -> runJob(message), ctx);
        f.onSuccess(new OnSuccess<Object>() {
            @Override
            public void onSuccess(Object result) throws Throwable {
                log.info("Notify Worker job status {}", result);
                sender.tell(result, getSelf());
                abortLoop.cancel();
            }
        }, ctx);
        f.onFailure(new OnFailure() {
            @Override
            public void onFailure(Throwable throwable) throws Throwable {
                log.error(throwable.toString());
                abortLoop.cancel();
                unhandled(message);
            }
        }, ctx);
        //getSender().tell(runJob(message));
    } else if (message instanceof Master.FileJob) {
        Master.FileJob fileJob = (Master.FileJob) message;
        try {
            if (fileJob.content != null) {
                FileUtils.touch(
                        new File(agentConfig.getJob().getPath(), fileJob.uploadFileRequest.getFileName()));
                FileUtils.writeStringToFile(
                        new File(agentConfig.getJob().getPath(), fileJob.uploadFileRequest.getFileName()),
                        fileJob.content);
                getSender().tell(new Worker.FileUploadComplete(fileJob.uploadFileRequest, HostUtils.lookupIp()),
                        getSelf());
            } else if (fileJob.remotePath != null) {
                FileUtils.touch(
                        new File(agentConfig.getJob().getPath(), fileJob.uploadFileRequest.getFileName()));
                FileUtils.copyURLToFile(new URL(fileJob.remotePath),
                        new File(agentConfig.getJob().getPath(), fileJob.uploadFileRequest.getFileName()));
                getSender().tell(new Worker.FileUploadComplete(fileJob.uploadFileRequest, HostUtils.lookupIp()),
                        getSelf());
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

From source file:com.ssn.listener.SSNFacebookAlbumSelectionListener.java

private void createComponents(SSNHomeForm ssnHomeForm, List<Photo> completePhotoList, SSNAlbumNode albumNode) {
    SSNHelper.toggleDeleteAndShareImages(false, ssnHomeForm);

    try {/*from   ww w .  j a v  a 2s  .c o m*/
        List<File> listOfFiles = new ArrayList<File>();

        File facebookPhotosDir = new File(
                SSNHelper.getFacebookPhotosDirPath() + albumNode.getAlbum().getName() + File.separator);
        if (!facebookPhotosDir.exists()) {
            facebookPhotosDir.mkdir();
        }

        for (Photo photo : completePhotoList) {
            String imageUrl = "";
            for (Image image : photo.getImages()) {
                if (image != null && image.getHeight() <= 500) {
                    imageUrl = image.getSource();
                    break;
                }
            }

            if (imageUrl.isEmpty()) {
                imageUrl = photo.getSource();
            }
            URL url = new URL(imageUrl);
            File file = new File(facebookPhotosDir.getAbsolutePath() + File.separator + photo.getId() + ".jpg");
            if (!file.exists()) {
                try {
                    FileUtils.copyURLToFile(url, file);
                    listOfFiles.add(file);
                } catch (Exception e) {
                    logger.error(e);
                }
            } else {
                listOfFiles.add(file);
            }
        }

        File[] fileArray = listOfFiles.toArray(new File[0]);
        SSNGalleryHelper contentPane = new SSNGalleryHelper(fileArray, ssnHomeForm);
        contentPane.setBackground(SSNConstants.SSN_BLACK_BACKGROUND_COLOR);

        ssnHomeForm.getSsnHomeCenterPanel().removeAll();
        ssnHomeForm.getSsnHomeCenterMainPanel().removeAll();

        ssnHomeForm.getSsnHomeCenterPanel().add(ssnHomeForm.getScrollPane(contentPane,
                SSNHelper.getAlbumNameFromPath(ssnHomeForm.ssnFileExplorer.m_display.getText())));
        ssnHomeForm
                .getSsnHomeCenterMainPanel().add(
                        ssnHomeForm.getSortPanel("Date", false,
                                SSNHelper
                                        .getAlbumNameFromPath(ssnHomeForm.ssnFileExplorer.m_display.getText())),
                        BorderLayout.NORTH);
        ssnHomeForm.getSsnHomeCenterMainPanel().add(ssnHomeForm.getSsnHomeCenterPanel(), BorderLayout.CENTER);
        ssnHomeForm.getHomeModel().getSSNMediaFolderProperties(
                ssnHomeForm.getHomeModel().getHomeForm().ssnFileExplorer.m_display.getText(), fileArray);
        // ssnHomeForm.add(ssnHomeForm.getSsnHomeCenterMainPanel());

        //             ssnHomeForm.getSplitPane().setLeftComponent(ssnHomeForm.getSsnHomeLeftMainPanel());
        //             ssnHomeForm.getSplitPane().setRightComponent(ssnHomeForm.getSsnHomeCenterMainPanel());
        //                //ssnHomeForm.getSplitPane().setDividerLocation(200);
        //             ssnHomeForm.getSplitPane().revalidate();
        //             ssnHomeForm.getSplitPane().repaint();

        ssnHomeForm.revalidate();
        ssnHomeForm.repaint();
        ssnHomeForm.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:minecrunch_launcher.Client.java

public void run() {
    // Reading modpack profile XML file and getting profile parameters
    if (os.contains("Windows")) {
        minecraftDir = home + "\\AppData\\Roaming\\.minecraft\\";
        tempDir = home + "\\AppData\\temp\\";
        clientInstall = tempDir + "client_install\\";
        modinstallDir = home + "\\AppData\\Roaming\\.minecraft\\minecrunch\\";
    }//from w w w . j a  va  2  s  . c om

    if (os.contains("Linux")) {
        minecraftDir = home + "/.minecraft/";
        tempDir = home + "/temp/";
        clientInstall = tempDir + "client_install/";
        modinstallDir = home + "/.minecraft/minecrunch/";
    }

    if (os.contains("Mac")) {
        minecraftDir = home + "/Library/Application Support/minecraft/";
        tempDir = home + "/temp/";
        clientInstall = tempDir + "client_install/";
        modinstallDir = home + "/Library/Application Support/minecraft/minecrunch/";
    }

    try {
        Modprofile();
    } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }

    // Install selected modpack
    wd.setVisible(true);
    System.out.println("Modpack picked: " + name);
    System.out.println("Gamename is: " + gamename);
    File dir = new File(tempDir);
    if (!dir.exists()) {
        if (dir.mkdir()) {
            String console = "Temporary directory created.";
            System.out.println(console);
        } else {
            String console = "Temporary directory already exists.";
            System.out.println(console);
        }
    }
    URL url = null;
    try {
        url = new URL("http://www.minecrunch.net/download/" + name + "/client_install.zip");
    } catch (MalformedURLException ex) {
        System.out.println(ex);
    }
    File file = new File(tempDir + "client_install.zip");
    try {
        FileUtils.copyURLToFile(url, file);
    } catch (IOException ex) {
        System.out.println(ex);
    }
    try {
        ZipFile zipFile = new ZipFile(tempDir + "client_install.zip");
        zipFile.extractAll(tempDir);
    } catch (ZipException e) {
    }
    file.delete();

    File newlib = new File(clientInstall + "libraries");
    File oldlib = new File(minecraftDir + "libraries");
    try {
        FileUtils.copyDirectory(newlib, oldlib);
        System.out.println("Copied libraries directory.");
        FileUtils.deleteDirectory(newlib);
        System.out.println("Deleted libraries directory.");
    } catch (IOException ex) {
        System.out.println(ex);
    }

    File newver = new File(clientInstall + "versions");
    File oldver = new File(minecraftDir + "versions");
    try {
        FileUtils.copyDirectory(newver, oldver);
        System.out.println("Copied version directory.");
        FileUtils.deleteDirectory(newver);
        System.out.println("Deleted version directory.");
    } catch (IOException ex) {
        System.out.println(ex);
    }

    File newmods = new File(clientInstall);
    File oldmods = new File(modinstallDir + name + "\\");
    try {
        FileUtils.copyDirectory(newmods, oldmods);
        System.out.println("Copied all other directories.");
    } catch (IOException ex) {
        System.out.println(ex);
    }

    try {
        // delete temporary directory
        FileUtils.deleteDirectory(dir);
    } catch (IOException ex) {
        System.out.println(ex);
    }

    // Setup profile
    String profile = gamename;
    String gamedir = modinstallDir + name;
    String filePath = minecraftDir + "launcher_profiles.json";
    JSONParser parser = new JSONParser();
    Object obj = null;
    try {
        obj = parser.parse(new FileReader(filePath));
    } catch (IOException | ParseException ex) {
    }
    JSONObject jsonObject = (JSONObject) obj;
    JSONObject profiles = (JSONObject) jsonObject.get("profiles");
    String selectedProfile = profile;
    String clientToken = (String) jsonObject.get("clientToken");
    JSONObject authenticationDatabase = (JSONObject) jsonObject.get("authenticationDatabase");
    JSONObject params = new JSONObject();
    params.put("name", profile);
    params.put("gameDir", gamedir);
    params.put("lastVersionId", version);
    params.put("javaArgs", java);
    profiles.put(profile, params);
    JSONObject update = new JSONObject();
    update.put("profiles", profiles);
    update.put("selectedProfile", selectedProfile);
    update.put("clientToken", clientToken);
    update.put("authenticationDatabase", authenticationDatabase);
    try (FileWriter newfile = new FileWriter(filePath)) {
        newfile.write(update.toJSONString());
        newfile.flush();
    } catch (IOException ex) {
        System.out.println(ex);
    }
    wd.setVisible(false);
}

From source file:hudson.cli.CLITest.java

License:asdf

@Issue("JENKINS-41745")
@Test/* w w  w . j av a2s .  c om*/
public void strictHostKey() throws Exception {
    home = tempHome();
    grabCliJar();

    r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
    r.jenkins.setAuthorizationStrategy(
            new MockAuthorizationStrategy().grant(Jenkins.ADMINISTER).everywhere().to("admin"));
    SSHD.get().setPort(0);
    File privkey = tmp.newFile("id_rsa");
    FileUtils.copyURLToFile(CLITest.class.getResource("id_rsa"), privkey);
    User.get("admin")
            .addProperty(new UserPropertyImpl(IOUtils.toString(CLITest.class.getResource("id_rsa.pub"))));
    assertNotEquals(0, new Launcher.LocalLauncher(StreamTaskListener.fromStderr()).launch()
            .cmds("java", "-Duser.home=" + home, "-jar", jar.getAbsolutePath(), "-s", r.getURL().toString(),
                    "-ssh", "-user", "admin", "-i", privkey.getAbsolutePath(), "-strictHostKey", "who-am-i")
            .stdout(System.out).stderr(System.err).join());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    assertEquals(0, new Launcher.LocalLauncher(StreamTaskListener.fromStderr()).launch()
            .cmds("java", "-Duser.home=" + home, "-jar", jar.getAbsolutePath(), "-s", r.getURL().toString(),
                    "-ssh", "-user", "admin", "-i", privkey.getAbsolutePath(), "-logger", "FINEST", "who-am-i")
            .stdout(baos).stderr(System.err).join());
    assertThat(baos.toString(), containsString("Authenticated as: admin"));
    baos = new ByteArrayOutputStream();
    assertEquals(0,
            new Launcher.LocalLauncher(StreamTaskListener.fromStderr()).launch()
                    .cmds("java", "-Duser.home=" + home, "-jar", jar.getAbsolutePath(), "-s",
                            r.getURL().toString()./* just checking */replaceFirst("/$", ""), "-ssh", "-user",
                            "admin", "-i", privkey.getAbsolutePath(), "-strictHostKey", "who-am-i")
                    .stdout(baos).stderr(System.err).join());
    assertThat(baos.toString(), containsString("Authenticated as: admin"));
}

From source file:com.assemblade.opendj.OpenDJUtils.java

private static void extractFileIfNeeded(String file, String destination) throws IOException {
    File destinationFile = new File(destination);
    if (!destinationFile.exists()) {
        FileUtils.copyURLToFile(OpenDJUtils.class.getClassLoader().getResource(file), destinationFile);
    }/*from  w w w  .  ja  v a 2 s  .  com*/
}

From source file:com.linkedin.pinot.tools.Quickstart.java

public boolean execute() throws Exception {
    _quickStartDataDir = new File("quickStartData" + System.currentTimeMillis());
    String quickStartDataDirName = _quickStartDataDir.getName();

    if (!_quickStartDataDir.exists()) {
        _quickStartDataDir.mkdir();//from   w  ww. j a  v  a  2 s .c o m
    }

    File schemaFile = new File(quickStartDataDirName + "/baseball.schema");
    File dataFile = new File(quickStartDataDirName + "/baseball.csv");
    File tableCreationJsonFileName = new File(quickStartDataDirName + "/baseballTable.json");

    FileUtils.copyURLToFile(Quickstart.class.getClassLoader().getResource("sample_data/baseball.schema"),
            schemaFile);
    FileUtils.copyURLToFile(Quickstart.class.getClassLoader().getResource("sample_data/baseball.csv"),
            dataFile);
    FileUtils.copyURLToFile(Quickstart.class.getClassLoader().getResource("sample_data/baseballTable.json"),
            tableCreationJsonFileName);

    File tempDirOne = new File("/tmp/" + System.currentTimeMillis());
    tempDirOne.mkdir();

    File tempDir = new File("/tmp/" + String.valueOf(System.currentTimeMillis()));
    String tableName = "baseballStats";
    QuickstartTableRequest request = new QuickstartTableRequest(tableName, schemaFile,
            tableCreationJsonFileName, _quickStartDataDir, FileFormat.CSV);
    final QuickstartRunner runner = new QuickstartRunner(Lists.newArrayList(request), 1, 1, 1, tempDir);

    runner.clean();
    runner.startAll();
    printStatus(color.CYAN, "Deployed Zookeeper");
    printStatus(color.CYAN, "Deployed controller, broker and server");
    runner.addSchema();
    printStatus(color.CYAN, "Added baseballStats schema");
    runner.addTable();
    printStatus(color.CYAN, "Creating baseballStats table");
    runner.buildSegment();
    printStatus(color.CYAN, "Built index segment for baseballStats");
    runner.pushSegment();
    printStatus(color.CYAN, "Pushing segments to the controller");
    printStatus(color.CYAN, "Waiting for a second for the server to fetch the assigned segment");

    Thread.sleep(5000);

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                printStatus(color.GREEN, "***** shutting down offline quick start *****");
                FileUtils.deleteDirectory(_quickStartDataDir);
                runner.clean();
                runner.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    printStatus(color.YELLOW, "Offline quickstart complete");

    String q1 = "select count(*) from baseballStats limit 0";
    printStatus(color.YELLOW, "Total number of documents in the table");
    printStatus(color.CYAN, "Query : " + q1);
    printStatus(color.YELLOW, prettyprintResponse(runner.runQuery(q1)));
    printStatus(color.GREEN, "***************************************************");

    String q2 = "select sum('runs') from baseballStats group by playerName top 5 limit 0";
    printStatus(color.YELLOW, "Top 5 run scorers of all time ");
    printStatus(color.CYAN, "Query : " + q2);
    printStatus(color.YELLOW, prettyprintResponse(runner.runQuery(q2)));
    printStatus(color.GREEN, "***************************************************");

    String q3 = "select sum('runs') from baseballStats where yearID=2000 group by playerName top 5 limit 0";
    printStatus(color.YELLOW, "Top 5 run scorers of the year 2000");
    printStatus(color.CYAN, "Query : " + q3);
    printStatus(color.YELLOW, prettyprintResponse(runner.runQuery(q3)));
    printStatus(color.GREEN, "***************************************************");

    String q4 = "select sum('runs') from baseballStats where yearID>=2000 group by playerName limit 0";
    printStatus(color.YELLOW, "Top 10 run scorers after 2000");
    printStatus(color.CYAN, "Query : " + q4);
    printStatus(color.YELLOW, prettyprintResponse(runner.runQuery(q4)));
    printStatus(color.GREEN, "***************************************************");

    String q5 = "select playerName,runs,homeRuns from baseballStats order by yearID limit 10";
    printStatus(color.YELLOW,
            "Print playerName,runs,homeRuns for 10 records from the table and order them by yearID");
    printStatus(color.CYAN, "Query : " + q5);
    printStatus(color.YELLOW, prettyprintResponse(runner.runQuery(q5)));
    printStatus(color.GREEN, "***************************************************");

    printStatus(color.GREEN,
            "you can always go to http://localhost:9000/query/ to play around in the query console");

    long st = System.currentTimeMillis();
    while (true) {
        if (System.currentTimeMillis() - st >= (60 * 60) * 1000) {
            break;
        }
    }

    printStatus(color.YELLOW, "running since an hour, stopping now");
    return true;
}

From source file:cz.cas.lib.proarc.common.workflow.WorkflowManagerTest.java

private WorkflowManager initWorkflowManager(String profilesResourceName) throws Exception {
    File xmlWorkflow = temp.newFile("workflowTest.xml");
    FileUtils.copyURLToFile(WorkflowManagerTest.class.getResource(profilesResourceName), xmlWorkflow);
    WorkflowProfiles.setInstance(new WorkflowProfiles(xmlWorkflow));
    wp = WorkflowProfiles.getInstance();
    AppConfiguration config = AppConfigurationFactory.getInstance().create(new HashMap<String, String>() {
        {//from  w ww . ja v a2  s  .  c  om
            put(AppConfiguration.PROPERTY_APP_HOME, temp.getRoot().getPath());
        }
    });
    UserManager users = UserUtil.createUserManagerPostgressImpl(config, null, daos);
    WorkflowManager wm = new WorkflowManager(wp, daos, users);
    return wm;
}

From source file:gethanselminutes.GetHanselminutes.java

private void jbDownloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbDownloadActionPerformed
    long startTime = System.nanoTime();
    jbDownload.setEnabled(false);// w ww .  j  av  a2  s  .c  o  m
    int iStart = (Integer) spStart.getValue();
    int iEnd = (Integer) spEnd.getValue();
    int nEpisodes = iStart - iEnd + 1;
    jlInfo.setText("Downloading " + nEpisodes + " episodes...");
    Thread t = new Thread(() -> {
        try {
            for (int i = iStart; i >= iEnd; i--) {
                FileUtils.copyURLToFile(new URL(BASE_URL + "0" + i + ".mp3"),
                        new File("Hanselminutes 0" + i + ".mp3"));
                jlInfo.setText("Finished " + (nEpisodes - (i - iEnd)) + " of " + nEpisodes);
            }
        } catch (MalformedURLException ex) {
            Logger.getLogger(GetHanselminutes.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(GetHanselminutes.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            jbDownload.setEnabled(true);
            long endTime = System.nanoTime();
            long dt_s = (long) ((endTime - startTime) / 1e9);
            jlInfo.setText(jlInfo.getText() + ". Time [s] = " + dt_s);
        }
    });
    t.start();
}

From source file:ImportPhagelist.java

private static String Download() throws IOException {
    String path = "http://phagesdb.org/data/?set=seq&type=full";
    String base = new File("").getAbsolutePath();
    String name = base + "/Fastas/PhagesDB_Data.txt";
    File file = new File(name);
    URL netPath = new URL(path);
    FileUtils.copyURLToFile(netPath, file);
    return file.toString();
}

From source file:com.etsy.arbiter.ArbiterTest.java

private File writeToTempFile(String resourceName) throws IOException {
    URL resource = getClass().getClassLoader().getResource(resourceName);
    if (resource == null) {
        throw new RuntimeException("Unable to load resource " + resourceName);
    }/*from   www . j  a  v a 2 s. co m*/

    File tempFile = temporaryFolder.newFile();
    FileUtils.copyURLToFile(resource, tempFile);

    return tempFile;
}