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:me.rgcjonas.portableMinecraftLauncher.Main.java

/**
 * entry point/*w ww. j  a  va2s  .  c o  m*/
 * @param args
 */
public static void main(String[] args) {
    File workDir = getWorkingDirectory();

    //ensure the working directory exists
    if (!workDir.exists()) {
        workDir.mkdir();
    }
    File launcherJar = new File(workDir, "launcher.jar");

    //download launcher if it doesn't exist
    if (!launcherJar.exists()) {
        try {
            URL launcherurl = new URL("https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft.jar");
            FileUtils.copyURLToFile(launcherurl, launcherJar);
        } catch (IOException e) {
            // shouldn't happen
            e.printStackTrace();
        }
    }

    URL[] urls;
    try {
        urls = new URL[] { launcherJar.toURI().toURL() };

        //this class loader mustn't be disposed while the launcher is running
        @SuppressWarnings("resource")
        LauncherClassLoader loader = new LauncherClassLoader(urls);

        //run it      
        Class<?> launcherFrame = loader.loadClass("net.minecraft.LauncherFrame");
        launcherFrame.getMethod("main", String[].class).invoke(null, (Object) args);
    } catch (Exception e) {
        // there's nothing we can do in that case but notify the dev
        e.printStackTrace();
    }
}

From source file:flashcrawler.FlashCrawler.java

/**
 * @param args the command line arguments
 *//*from  www.j  a va  2s.c  o  m*/
public static void main(String[] args) throws FileNotFoundException {
    Scanner scn = new Scanner(new File("input.txt"));
    ArrayList<String> ins = new ArrayList();
    while (scn.hasNextLine()) {
        String input = scn.nextLine();
        ins.add(input);
    }

    String URL;
    PrintWriter writer = null;
    writer = new PrintWriter(new FileOutputStream(new File("error-log.txt"), true));

    String File;
    for (String name : ins) {
        File offlinePath = new File("/games/" + name + ".swf");
        String onlinePath = "http://wsh.gamib.com/x/" + name + "/" + name + ".swf";
        System.out.println("Downloading " + onlinePath + " into " + offlinePath);
        URL url = null;
        try {
            System.out.println("...");
            url = new URL(onlinePath);
        } catch (MalformedURLException ex) {
            System.out.println("Failed to create url object");
            writer.println("Error when creating url: " + onlinePath + "\tname");
        }
        try {
            System.out.println("...");
            FileUtils.copyURLToFile(url, offlinePath);
            System.out.println("Success.");
        } catch (IOException ex) {
            System.out.println("Error when downloading game: " + offlinePath);
            writer.println("Error when downloading game: " + offlinePath);
        }

    }
    writer.close();
    System.out.println("Process complete!");
}

From source file:at.co.malli.relpm.RelPM.java

/**
 * @param args the command line arguments
 *///from w ww.  ja  v  a 2 s  . c o  m
public static void main(String[] args) {
    //<editor-fold defaultstate="collapsed" desc=" Create config & log directory ">
    String userHome = System.getProperty("user.home");
    File relPm = new File(userHome + "/.RelPM");
    if (!relPm.exists()) {
        boolean worked = relPm.mkdir();
        if (!worked) {
            ExceptionDisplayer.showErrorMessage(new Exception("Could not create directory "
                    + relPm.getAbsolutePath() + " to store user-settings and logs"));
            System.exit(-1);
        }
    }
    File userConfig = new File(relPm.getAbsolutePath() + "/RelPM-userconfig.xml"); //should be created...
    if (!userConfig.exists()) {
        try {
            URL resource = RelPM.class.getResource("/at/co/malli/relpm/RelPM-defaults.xml");
            FileUtils.copyURLToFile(resource, userConfig);
        } catch (IOException ex) {
            ExceptionDisplayer.showErrorMessage(new Exception("Could not copy default config. Reason:\n"
                    + ex.getClass().getName() + ": " + ex.getMessage()));
            System.exit(-1);
        }
    }
    if (!userConfig.canWrite() || !userConfig.canRead()) {
        ExceptionDisplayer.showErrorMessage(new Exception(
                "Can not read or write " + userConfig.getAbsolutePath() + "to store user-settings"));
        System.exit(-1);
    }
    if (System.getProperty("os.name").toLowerCase().contains("win")) {
        Path relPmPath = Paths.get(relPm.toURI());
        try {
            Files.setAttribute(relPmPath, "dos:hidden", true);
        } catch (IOException ex) {
            ExceptionDisplayer.showErrorMessage(new Exception("Could not set " + relPm.getAbsolutePath()
                    + " hidden. " + "Reason:\n" + ex.getClass().getName() + ": " + ex.getMessage()));
            System.exit(-1);
        }
    }
    //</editor-fold>

    logger.trace("Environment setup sucessfull");

    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code ">
    try {
        String wantedLookAndFeel = SettingsProvider.getInstance().getString("gui.lookAndFeel");
        UIManager.LookAndFeelInfo[] installed = UIManager.getInstalledLookAndFeels();
        boolean found = false;
        for (UIManager.LookAndFeelInfo info : installed) {
            if (info.getClassName().equals(wantedLookAndFeel))
                found = true;
        }
        if (found)
            UIManager.setLookAndFeel(wantedLookAndFeel);
        else
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException ex) {
        logger.error(ex.getMessage());
        ExceptionDisplayer.showErrorMessage(ex);
    } catch (InstantiationException ex) {
        logger.error(ex.getMessage());
        ExceptionDisplayer.showErrorMessage(ex);
    } catch (IllegalAccessException ex) {
        logger.error(ex.getMessage());
        ExceptionDisplayer.showErrorMessage(ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        logger.error(ex.getMessage());
        ExceptionDisplayer.showErrorMessage(ex);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Add GUI start to awt EventQue ">
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MainGUI().setVisible(true);
        }
    });
    //</editor-fold>
}

From source file:com.atlauncher.Bootstrap.java

public static void main(String[] args) {
    String json = null;//from  www  . j av a 2  s  .  c om

    if (!Files.isDirectory(PATH)) {
        try {
            Files.createDirectories(PATH);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    try {
        json = IOUtils.toString(URI.create("http://download.nodecdn.net/containers/atl/v4/app.json"));
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }

    Application application = GSON.fromJson(json, Application.class);

    Dependency currentDependency = null;

    if (Files.exists(PATH.resolve("nwjs.json"))) {
        try {
            currentDependency = GSON.fromJson(FileUtils.readFileToString(PATH.resolve("nwjs.json").toFile()),
                    Dependency.class);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    for (Dependency dependency : application.getDependencies()) {
        if (dependency.shouldInstall() && !dependency.matches(currentDependency)) {
            if (Files.isDirectory(PATH.resolve("nwjs/"))) {
                try {
                    FileUtils.deleteDirectory(PATH.resolve("nwjs/").toFile());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            try {
                File zipFile = PATH.resolve("nwjs/temp.zip").toFile();
                FileUtils.copyURLToFile(dependency.getUrl(), zipFile);
                Utils.unzip(zipFile, PATH.resolve("nwjs/").toFile());
                FileUtils.forceDelete(zipFile);

                currentDependency = dependency;

                FileUtils.writeStringToFile(PATH.resolve("nwjs.json").toFile(), GSON.toJson(dependency));

                break;
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }

    App currentApp = null;

    if (Files.exists(PATH.resolve("app.json"))) {
        try {
            currentApp = GSON.fromJson(FileUtils.readFileToString(PATH.resolve("app.json").toFile()),
                    App.class);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    if (!application.getApp().matches(currentApp)
            || (currentApp != null && !Files.exists(PATH.resolve(currentApp.getFilename())))) {
        if (currentApp != null && Files.exists(PATH.resolve(currentApp.getFilename()))) {
            try {
                FileUtils.forceDelete(PATH.resolve(currentApp.getFilename()).toFile());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            FileUtils.copyURLToFile(application.getApp().getUrl(),
                    PATH.resolve(application.getApp().getFilename()).toFile());

            currentApp = application.getApp();

            FileUtils.writeStringToFile(PATH.resolve("app.json").toFile(), GSON.toJson(application.getApp()));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    if (currentDependency == null || currentApp == null) {
        System.exit(1);
    }

    List<String> arguments = new ArrayList<>();
    arguments.add(PATH.resolve("nwjs/").toAbsolutePath() + currentDependency.getStartup());
    arguments.add(currentApp.getFilename());
    ProcessBuilder processBuilder = new ProcessBuilder(arguments);
    processBuilder.directory(PATH.toFile());
    try {
        processBuilder.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:hdfs.MiniHDFS.java

public static void main(String[] args) throws Exception {
    if (args.length != 1 && args.length != 3) {
        throw new IllegalArgumentException(
                "Expected: MiniHDFS <baseDirectory> [<kerberosPrincipal> <kerberosKeytab>], " + "got: "
                        + Arrays.toString(args));
    }/*from  w  w w.j ava 2 s  .c  o  m*/
    boolean secure = args.length == 3;

    // configure Paths
    Path baseDir = Paths.get(args[0]);
    // hadoop-home/, so logs will not complain
    if (System.getenv("HADOOP_HOME") == null) {
        Path hadoopHome = baseDir.resolve("hadoop-home");
        Files.createDirectories(hadoopHome);
        System.setProperty("hadoop.home.dir", hadoopHome.toAbsolutePath().toString());
    }
    // hdfs-data/, where any data is going
    Path hdfsHome = baseDir.resolve("hdfs-data");

    // configure cluster
    Configuration cfg = new Configuration();
    cfg.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, hdfsHome.toAbsolutePath().toString());
    // lower default permission: TODO: needed?
    cfg.set(DFSConfigKeys.DFS_DATANODE_DATA_DIR_PERMISSION_KEY, "766");

    // optionally configure security
    if (secure) {
        String kerberosPrincipal = args[1];
        String keytabFile = args[2];

        cfg.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
        cfg.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHORIZATION, "true");
        cfg.set(DFSConfigKeys.DFS_NAMENODE_KERBEROS_PRINCIPAL_KEY, kerberosPrincipal);
        cfg.set(DFSConfigKeys.DFS_DATANODE_KERBEROS_PRINCIPAL_KEY, kerberosPrincipal);
        cfg.set(DFSConfigKeys.DFS_WEB_AUTHENTICATION_KERBEROS_PRINCIPAL_KEY, kerberosPrincipal);
        cfg.set(DFSConfigKeys.DFS_NAMENODE_KEYTAB_FILE_KEY, keytabFile);
        cfg.set(DFSConfigKeys.DFS_DATANODE_KEYTAB_FILE_KEY, keytabFile);
        cfg.set(DFSConfigKeys.DFS_NAMENODE_ACLS_ENABLED_KEY, "true");
        cfg.set(DFSConfigKeys.DFS_BLOCK_ACCESS_TOKEN_ENABLE_KEY, "true");
        cfg.set(DFSConfigKeys.IGNORE_SECURE_PORTS_FOR_TESTING_KEY, "true");
    }

    UserGroupInformation.setConfiguration(cfg);

    // TODO: remove hardcoded port!
    MiniDFSCluster.Builder builder = new MiniDFSCluster.Builder(cfg);
    if (secure) {
        builder.nameNodePort(9998);
    } else {
        builder.nameNodePort(9999);
    }
    MiniDFSCluster dfs = builder.build();

    // Configure contents of the filesystem
    org.apache.hadoop.fs.Path esUserPath = new org.apache.hadoop.fs.Path("/user/elasticsearch");
    try (FileSystem fs = dfs.getFileSystem()) {

        // Set the elasticsearch user directory up
        fs.mkdirs(esUserPath);
        if (UserGroupInformation.isSecurityEnabled()) {
            List<AclEntry> acls = new ArrayList<>();
            acls.add(new AclEntry.Builder().setType(AclEntryType.USER).setName("elasticsearch")
                    .setPermission(FsAction.ALL).build());
            fs.modifyAclEntries(esUserPath, acls);
        }

        // Install a pre-existing repository into HDFS
        String directoryName = "readonly-repository";
        String archiveName = directoryName + ".tar.gz";
        URL readOnlyRepositoryArchiveURL = MiniHDFS.class.getClassLoader().getResource(archiveName);
        if (readOnlyRepositoryArchiveURL != null) {
            Path tempDirectory = Files.createTempDirectory(MiniHDFS.class.getName());
            File readOnlyRepositoryArchive = tempDirectory.resolve(archiveName).toFile();
            FileUtils.copyURLToFile(readOnlyRepositoryArchiveURL, readOnlyRepositoryArchive);
            FileUtil.unTar(readOnlyRepositoryArchive, tempDirectory.toFile());

            fs.copyFromLocalFile(true, true,
                    new org.apache.hadoop.fs.Path(
                            tempDirectory.resolve(directoryName).toAbsolutePath().toUri()),
                    esUserPath.suffix("/existing/" + directoryName));

            FileUtils.deleteDirectory(tempDirectory.toFile());
        }
    }

    // write our PID file
    Path tmp = Files.createTempFile(baseDir, null, null);
    String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
    Files.write(tmp, pid.getBytes(StandardCharsets.UTF_8));
    Files.move(tmp, baseDir.resolve(PID_FILE_NAME), StandardCopyOption.ATOMIC_MOVE);

    // write our port file
    tmp = Files.createTempFile(baseDir, null, null);
    Files.write(tmp, Integer.toString(dfs.getNameNodePort()).getBytes(StandardCharsets.UTF_8));
    Files.move(tmp, baseDir.resolve(PORT_FILE_NAME), StandardCopyOption.ATOMIC_MOVE);
}

From source file:com.hw13c.HttpClientPost.java

public static void main(String[] args) {
    output = new File("out");
    input = new File("in");
    try {//  w  w  w  .  j  a v a  2 s .  co m
        //   URL urls = new URL("http://www.bbc.com/sport/0/sports-personality/30267315");
        // 1.   URL urls = new URL("http://abcnews.go.com/US/nypd-officer-indicted-eric-garner-choke-hold-death/story?id=27341079");
        URL urls = new URL(
                "http://www.chron.com/news/us/article/UN-campaign-seeks-64-million-for-Syrian-refugees-5932973.php");

        //URL urls = new URL("http://www.bbc.com/news/health-30254697");
        FileUtils.copyURLToFile(urls, input);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //  verifyArgs(args);
    HttpClientPost httpClientPost = new HttpClientPost();
    //httpClientPost.input = new File(args[0]);
    //httpClientPost.output = new File(args[1]);
    httpClientPost.client = new HttpClient();
    httpClientPost.client.getParams().setParameter("http.useragent", "Calais Rest Client");

    httpClientPost.run();
    try {
        queryRDFTriples();
    } catch (RepositoryException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (RDFParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:br.org.ipti.guigoh.util.DownloadService.java

public static void downloadFileFromURL(String filePath, String fileType)
        throws MalformedURLException, IOException {
    File file = new File(filePath);
    URL url = new URL(!filePath.contains("http") ? "http://" + filePath : filePath);
    FileUtils.copyURLToFile(url, new File(filePath));
    downloadFile(file, fileType);//from  w  w w  . j  a  va2 s  .c  om
}

From source file:com.teradata.presto.yarn.test.utils.Resources.java

public static Path extractResource(String resourcePath) {
    URL fixScriptResource = Resources.class.getResource(resourcePath);
    File temporaryFile = new File(tempDir, Paths.get(resourcePath).getFileName().toString());
    try {/*  w  w w  . j  a  va 2  s . c  om*/
        FileUtils.copyURLToFile(fixScriptResource, temporaryFile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return Paths.get(temporaryFile.toURI());
}

From source file:it.cnr.ilc.ilcioutils.IlcInputToFile.java

/**
 * Creates a file with the content read from the URL
 * @param theUrl the URL where the content is
 * @return a File with the content from the URL
 *///from  ww  w . ja  va  2  s .  com
public static File createAndWriteTempFileFromUrl(String theUrl) {
    File tempOutputFile = null;
    String message;
    try {
        tempOutputFile = File.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX);

        FileUtils.copyURLToFile(new URL(theUrl), tempOutputFile);
    } catch (IOException e) {
        message = String.format("Error in accessing URL %s %s", theUrl, e.getMessage());
        Logger.getLogger(CLASS_NAME).log(Level.SEVERE, message);
    }
    return tempOutputFile;

}

From source file:mxnet.ImageClassification.java

private static void downloadUrl(String url, String filePath) {
    File tmpFile = new File(filePath);
    if (!tmpFile.exists()) {
        try {/*  w  w w  .  j  a  va2  s .  c  o m*/
            FileUtils.copyURLToFile(new URL(url), tmpFile);
        } catch (Exception exception) {
            System.err.println(exception);
        }
    }
}