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.canova.image.mnist.MnistFetcher.java

public File downloadAndUntar() throws IOException {
    if (fileDir != null) {
        return fileDir;
    }/*w w  w .j  a  v  a 2 s .  c om*/
    // 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(tmpDir, LOCAL_DIR_NAME);
    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;
}

From source file:org.chemid.structure.dbclient.pubchem.PubChemClient.java

/**
 * @param fileUrl/*  w w w .jav  a 2 s.  c o m*/
 * @param location
 * @return sdf file saved location with file name
 * @throws ChemIDStructureException
 */
public String saveFile(String fileUrl, String location) throws ChemIDStructureException {
    String savedPath = null;
    if (fileUrl != null) {
        try {
            URL url = new URL(fileUrl);
            File dir = new File(location);
            dir.mkdirs();
            String fileName = new SimpleDateFormat(Constants.ZIP_FILE_NAME).format(new Date());
            File tmp = new File(dir, fileName);
            tmp.createNewFile();
            FileUtils.copyURLToFile(url, tmp);
            if (location.endsWith("/")) {
                savedPath = location + fileName;

            } else {
                savedPath = location + '/' + fileName;
            }
        } catch (IOException e) {
            throw new ChemIDStructureException("Error occurred while saving PubChem results file : ", e);

        }
    }
    return savedPath;
}

From source file:org.codehaus.preon.sample.bmp.BitmapFileTest.java

public void testHeightWidth() throws DecodingException, IOException {
    Codec<BitmapFile> codec = Codecs.create(BitmapFile.class);
    File file = File.createTempFile("test", ".bmp");
    file.deleteOnExit();//from w ww.  j a  v a2 s .  c  om
    FileUtils.copyURLToFile(BitmapFileTest.class.getClassLoader().getResource("test.bmp"), file);
    try {
        System.out.println(file.getAbsolutePath());
        BitmapFile bitmap = Codecs.decode(codec, file);
        assertEquals(48, bitmap.getHeight());
        assertEquals(48, bitmap.getWidth());
        for (RgbQuad quad : bitmap.getColors()) {
            System.out.print("Color ");
            System.out.print(quad.getRed());
            System.out.print(", ");
            System.out.print(quad.getGreen());
            System.out.print(", ");
            System.out.print(quad.getBlue());
            System.out.println();
        }
        System.out.println("Data " + bitmap.getData().length);
        File directory = new File(System.getProperty("java.io.tmpdir"));
        File document = new File(directory, "bitmap.html");
        System.out.println("Location of HTML document: " + document);
        Codecs.document(codec, DocumentType.Html, document);
    } finally {
        file.delete();
    }
}

From source file:org.commonjava.maven.ext.core.impl.JSONManipulatorTest.java

@Before
public void setup() throws IOException, IllegalAccessException {
    FieldUtils.writeField(jsonManipulator, "jsonIO", new JSONIO(), true);

    URL resource = JSONIOTest.class.getResource("npm-shrinkwrap.json");
    npmFile = tf.newFile();/*ww  w. ja v a  2s . c om*/
    pluginFile = tf.newFile();

    FileUtils.copyURLToFile(resource, npmFile);

    URL resource2 = JSONIOTest.class.getResource("amg-plugin-registry.json");
    FileUtils.copyURLToFile(resource2, pluginFile);
}

From source file:org.commonjava.maven.ext.core.impl.XMLManipulatorTest.java

@Before
public void setup() throws IOException, IllegalAccessException, URISyntaxException {
    URL resource = XMLIOTest.class.getResource("activemq-artemis-dep.xml");
    xmlFile = tf.newFile();//  www  .j a v a2 s.  c o m
    FileUtils.copyURLToFile(resource, xmlFile);
}

From source file:org.commonjava.maven.ext.io.FileIO.java

/**
 * Read the raw file from a given URL. Useful if we need to read
 * a remote file./*  www  .ja va 2 s  .  c om*/
 *
 * @param ref the ArtifactRef to read.
 * @return the file for the URL
 */
public File resolveURL(final URL ref) throws IOException {
    File cache = infra.getCacheDir();
    File result = new File(cache, UUID.randomUUID().toString());

    FileUtils.copyURLToFile(ref, result);

    return result;
}

From source file:org.commonjava.maven.ext.manip.impl.JSONManipulatorTest.java

@Before
public void setup() throws IOException, IllegalAccessException, URISyntaxException {
    FieldUtils.writeField(jsonManipulator, "jsonIO", new JSONIO(), true);

    URL resource = JSONIOTest.class.getResource("npm-shrinkwrap.json");
    npmFile = tf.newFile();/*  ww w . ja  v a2 s.co m*/
    pluginFile = tf.newFile();

    FileUtils.copyURLToFile(resource, npmFile);

    URL resource2 = JSONIOTest.class.getResource("amg-plugin-registry.json");
    FileUtils.copyURLToFile(resource2, pluginFile);
}

From source file:org.commonjava.maven.ext.manip.impl.XMLManipulatorTest.java

@Before
public void setup() throws IOException, IllegalAccessException, URISyntaxException {
    FieldUtils.writeField(xmlManipulator, "xmlIO", new XMLIO(), true);

    URL resource = XMLIOTest.class.getResource("activemq-artemis-dep.xml");
    xmlFile = tf.newFile();//w  w  w  . j  a  va  2 s.  c o m
    FileUtils.copyURLToFile(resource, xmlFile);
}

From source file:org.copperengine.ext.wfrepo.classpath.ClasspathWorkflowRepository.java

private Map<String, Clazz> findInterruptableMethods(final Set<Class<?>> wfSet, ClassLoader cl)
        throws IOException {
    logger.info("Analysing classfiles");
    // Find and visit all classes
    Map<String, Clazz> clazzMap = new HashMap<String, Clazz>();
    for (Class<?> c : wfSet) {
        logger.info("analysing class {}", c.getName());
        ScottyFindInterruptableMethodsVisitor visitor = new ScottyFindInterruptableMethodsVisitor();
        URL url = cl.getResource(c.getName().replace(".", "/") + ".class");
        InputStream is = url.openStream();
        try {//from w w w.ja  v a  2  s  . co m
            ClassReader cr = new ClassReader(is);
            cr.accept(visitor, 0);
        } finally {
            is.close();
        }
        Clazz clazz = new Clazz();
        clazz.interruptableMethods = visitor.getInterruptableMethods();
        clazz.classfile = url;
        clazz.classname = visitor.getClassname();
        clazz.superClassname = visitor.getSuperClassname();
        clazzMap.put(clazz.classname, clazz);
    }

    // Remove all classes that are no workflow
    List<String> allClassNames = new ArrayList<String>(clazzMap.keySet());
    for (String classname : allClassNames) {
        Clazz clazz = clazzMap.get(classname);
        Clazz startClazz = clazz;
        while (true) {
            startClazz.aggregatedInterruptableMethods.addAll(clazz.interruptableMethods);
            if ("org/copperengine/core/Workflow".equals(clazz.superClassname)
                    || "org/copperengine/core/persistent/PersistentWorkflow".equals(clazz.superClassname)) {
                break;
            }
            clazz = clazzMap.get(clazz.superClassname);
            if (clazz == null) {
                break;
            }
        }
        if (clazz == null) {
            // this is no workflow
            clazzMap.remove(classname);
            final URL url = cl.getResource(classname.replace(".", "/") + ".class");
            final File target = new File(adaptedTargetDir, classname.replace(".", "/") + ".class");
            target.getParentFile().mkdirs();

            FileUtils.copyURLToFile(url, target);
        }
    }

    return clazzMap;
}

From source file:org.cuff.util.Mailer.java

public void emailSubmission(Contributor c, Subject s, PhysicalAppearance pa, List<Warrant> warrants,
        List<Judgment> judgments, List<CriminalHistory> criminalHistory)
        throws InterruptedException, IOException, URISyntaxException {
    SendGrid sendgrid = new SendGrid(SENDGRID_API_KEY);

    Email email = new Email();
    email.addTo(SUBMISSION_RECEPIENT);/*from  w  w w.j  av a2s  .  c o  m*/
    email.setFrom(SUBMISSION_SENDER);
    email.setBcc(new String[] { SUBMISSION_BCC });

    // Add border
    StringBuilder warrantsTable = new StringBuilder(
            "<table><tr><td>Municipality</td><td>Charge</td><td>Warrant number</td><td>Issued</td></tr>");
    for (Warrant w : warrants) {
        warrantsTable.append("<tr>").append("<td>" + w.getMunicipality() + "</td>")
                .append("<td>" + w.getCharge() + "</td>").append("<td>" + w.getWarrantNumber() + "</td>")
                .append("<td>" + w.getMonth() + "/" + w.getYear() + "</td>").append("</tr>");
    }
    warrantsTable.append("</table>");

    StringBuilder judgmentsTable = new StringBuilder(
            "<table><tr><td>Municipality</td><td>Judgment number</td><td>Amount</td><td>Issued</td></tr>");
    for (Judgment j : judgments) {
        judgmentsTable.append("<tr>").append("<td>" + j.getMunicipality() + "</td>")
                .append("<td>" + j.getJudgmentNumber() + "</td>").append("<td>" + j.getAmount() + "</td>")
                .append("<td>" + j.getMonth() + "/" + j.getYear() + "</td>").append("</tr>");
    }
    judgmentsTable.append("</table>");

    System.out.println(warrantsTable);

    StringBuilder criminalHistoryTable = new StringBuilder(
            "<table><tr><td>Municipality</td><td>Charge</td><td>Issued</td></tr>");
    for (CriminalHistory ch : criminalHistory) {
        criminalHistoryTable.append("<tr>").append("<td>" + ch.getMunicipality() + "</td>")
                .append("<td>" + ch.getCharge() + "</td>")
                .append("<td>" + ch.getMonth() + "/" + ch.getYear() + "</td>").append("</tr>");
    }
    criminalHistoryTable.append("</table>");

    // Digest file
    String urlString = System.getenv("EMAIL_TEMPLATE_URL");
    URL url = new URL(urlString);
    File urlFile = new File("temp.txt");
    FileUtils.copyURLToFile(url, urlFile);
    String data = FileUtils.readFileToString(urlFile, "UTF-8");
    String emailContent = String.format(data, c.getFirstName(), c.getLastName(), c.getEmailAddress(),
            c.getPhoneNumber(), c.getPreferredContactMethod(), c.getRelationship(), c.getCity(), c.getState(),
            c.getZipcode(), c.getContactName(), c.getContactOrganization(), c.getContactCity(),
            c.getContactState(), c.getContactPhoneNumber(), c.getContactEmailAddress(), s.getFirstName(),
            s.getMiddleName(), s.getLastName(), s.getGender(), s.getAliases(), s.getDriversLicense(),
            s.getSsn(), s.getDob(), s.getBackgroundInfo(), s.getStates(), s.getVehicle(),
            s.getVehiclePlateNumber(), s.getVin(), s.getReward(), pa.getRace(), pa.getNationality(),
            pa.getHeight() / 12 + " feet, " + pa.getHeight() % 12 + " inches", pa.getWeight() + "lbs",
            pa.getHairColor(), pa.getEyeColor(), pa.getPhysicalCharacteristics(), warrantsTable.toString(),
            judgmentsTable.toString(), criminalHistoryTable.toString());
    System.out.println(emailContent);

    // SET TEMPLATE
    //       email.setTemplateId(System.getenv("SENDGRID_TEMPLATE_ID"));
    //       email.getSMTPAPI()
    //          .addSubstitution(":contributor_first_name", c.getFirstName())
    //          .addSubstitution(":contributor_last_name", c.getLastName())
    //          .addSubstitution(":contributor_email_address", c.getEmailAddress())
    //          .addSubstitution(":contributor_phone_number", c.getPhoneNumber())
    //          .addSubstitution(":contributor_preferred_contact_method", c.getPreferredContactMethod())
    //          .addSubstitution(":contributor_relationship_to_subject", c.getRelationship())
    //          .addSubstitution(":contributor_city", c.getCity())
    //          .addSubstitution(":contributor_state", c.getState())
    //          .addSubstitution(":contributor_zipcode", c.getZipcode())
    //          .addSubstitution(":contributor_contact_name", c.getCity())
    //          .addSubstitution(":contributor_contact_organization", c.getContactOrganization())
    //          .addSubstitution(":contributor_contact_city", c.getContactCity())
    //          .addSubstitution(":contributor_contact_state", c.getContactState())
    //          .addSubstitution(":contributor_contact_phone_number", c.getContactPhoneNumber())
    //          .addSubstitution(":contributor_contact_email_address", c.getContactEmailAddress())
    //          .addSubstitution(":first_name", s.getFirstName())
    //          .addSubstitution(":middle_name", s.getMiddleName())
    //          .addSubstitution(":last_name", s.getLastName())
    //          .addSubstitution(":aliases", s.getAliases())
    //          .addSubstitution(":gender", s.getGender())
    //          .addSubstitution(":drivers_license", s.getDriversLicense())
    //          .addSubstitution(":ssn", s.getSsn())
    //          .addSubstitution(":dob", s.getDob())
    //          .addSubstitution(":background_info", s.getBackgroundInfo())
    //          .addSubstitution(":race", pa.getRace())
    //          .addSubstitution(":nationality", pa.getNationality())
    //          .addSubstitution(":height", pa.getHeight()/12 + " feet, " +pa.getHeight()%12+" inches")
    //          .addSubstitution(":weight", pa.getWeight() + "lbs")
    //          .addSubstitution(":hair_color", pa.getHairColor())
    //          .addSubstitution(":eye_color", pa.getEyeColor())
    //          .addSubstitution(":physical_characteristics", pa.getPhysicalCharacteristics())
    //          .addSubstitution(":warrants_table", warrantsTable.toString())
    //          .addSubstitution(":judgments_table", judgmentsTable.toString())
    //          .addSubstitution(":criminal_history_table", criminalHistoryTable.toString())
    //          ;

    email.setSubject(c.getEntryType() + " online submission from " + c.getFirstName());
    email.setHtml(emailContent);
    Response response;

    try {
        // Email confirmation to contributor
        Email confirmationEmail = new Email();
        confirmationEmail.addTo(c.getEmailAddress());
        confirmationEmail.setFrom(SUBMISSION_SENDER);
        confirmationEmail.setBcc(new String[] { SUBMISSION_BCC });
        confirmationEmail.setSubject("Thank you for your online submission to CUFF");
        confirmationEmail.setHtml(
                "Thank you for your submission. If you'd like to send photos of the subject, please email mail@straightshooter.net. Please include the subject's first and last name, and the date of your entry.\n"
                        + emailContent);
        sendgrid.send(confirmationEmail);
    } catch (SendGridException e) {
    }

    try {
        response = sendgrid.send(email);
        System.out.println(response.getMessage());
    } catch (SendGridException e) {
        e.printStackTrace();
    }

}