Example usage for java.net URL openStream

List of usage examples for java.net URL openStream

Introduction

In this page you can find the example usage for java.net URL openStream.

Prototype

public final InputStream openStream() throws java.io.IOException 

Source Link

Document

Opens a connection to this URL and returns an InputStream for reading from that connection.

Usage

From source file:Utils.java

public static void download(String urlStr, File destFile) throws IOException {
    URL srcUrl = new URL(urlStr);

    InputStream input = null;// w w w  .  j  a v a  2 s .  c  om
    OutputStream output = null;
    try {
        input = srcUrl.openStream();
        FileOutputStream fos = new FileOutputStream(destFile);
        output = new BufferedOutputStream(fos);
        copyStreams(input, output);
    } finally {
        if (input != null) {
            input.close();
        }
        if (output != null) {
            output.flush();
            output.close();
        }
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.api.lexmorph.TagsetMappingFactory.java

public static Map<String, String> loadProperties(URL aUrl) {
    InputStream is = null;//from   w  ww  .  j  a  v  a 2 s  . com
    try {
        is = aUrl.openStream();
        Properties mappingProperties = new Properties();
        mappingProperties.load(is);
        Map<String, String> mapping = new HashMap<String, String>();
        for (String key : mappingProperties.stringPropertyNames()) {
            mapping.put(key.trim(), mappingProperties.getProperty(key).trim());
        }
        return mapping;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        closeQuietly(is);
    }
}

From source file:Main.java

/**
 * Return the text of the file with the given URL. E.g. if
 * http://test.be/text.txt is given the contents of text.txt is returned.
 * /*from w w w .j  av  a  2 s. co  m*/
 * @param url
 *            The URL.
 * @return The contents of the file.
 */
public static String readTextFromUrl(URL url) {
    StringBuffer fubber = new StringBuffer();
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            fubber.append(inputLine).append("\n");
        }
        in.close();
    } catch (IOException exception) {
        exception.printStackTrace();
    }
    return fubber.toString();
}

From source file:com.gemstone.gemfire.internal.logging.log4j.custom.CustomConfiguration.java

public static File createConfigFileIn(final File targetFolder) throws IOException, URISyntaxException {
    URL resource = openConfigResource();
    File targetFile = new File(targetFolder, CONFIG_FILE_NAME);
    IOUtils.copy(resource.openStream(), new FileOutputStream(targetFile));
    assertThat(targetFile).hasSameContentAs(new File(resource.toURI()));
    return targetFile;
}

From source file:data.repository.pragma.utils.MD5Utils.java

public static String getMD5(String file_url) {

    URL url;
    try {//from   ww  w . j a v a2  s. c  om
        url = new URL(file_url);
        InputStream is = url.openStream();
        MessageDigest md = MessageDigest.getInstance("MD5");
        String digest = getDigest(is, md, 2048);
        return digest;
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        return null;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        return null;
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        return null;
    }
}

From source file:Main.java

/**
 * getDocument w/URL//from w  w  w  .  ja va  2 s. co  m
 */
public static Document getDocument(final URL url)
        throws ParserConfigurationException, SAXException, IOException {
    return getDocument(url.openStream());
}

From source file:com.kurento.kmf.test.services.Recorder.java

public static float getPesqMos(String audio, int sampleRate) {
    float pesqmos = 0;

    try {//from ww w .  java2 s .c  o  m
        String pesq = KurentoServicesTestHelper.getTestFilesPath() + "/bin/pesq/PESQ";
        String origWav = "";
        if (audio.startsWith(HTTP_TEST_FILES)) {
            origWav = KurentoServicesTestHelper.getTestFilesPath() + audio.replace(HTTP_TEST_FILES, "");
        } else {
            // Download URL
            origWav = KurentoMediaServerManager.getWorkspace() + "/downloaded.wav";
            URL url = new URL(audio);
            ReadableByteChannel rbc = Channels.newChannel(url.openStream());
            FileOutputStream fos = new FileOutputStream(origWav);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            fos.close();
        }

        Shell.runAndWait(pesq, "+" + sampleRate, origWav, RECORDED_WAV);
        List<String> lines = FileUtils.readLines(new File(PESQ_RESULTS), "utf-8");
        pesqmos = Float.parseFloat(lines.get(1).split("\t")[2].trim());
        log.info("PESQMOS " + pesqmos);

        Shell.runAndWait("rm", PESQ_RESULTS);

    } catch (IOException e) {
        log.error("Exception recording local audio", e);
    }

    return pesqmos;
}

From source file:eu.scape_project.pc.droid.DroidIdentificationTest.java

/**
 * Set up.//w ww. j a v a2  s . c o  m
 * @throws Exception 
 */
@BeforeClass
public static void setUpClass() throws Exception {
    URL sigFileV67Url = new URL(SIGNATURE_FILE_V67_URL);
    InputStream sigFileStream = sigFileV67Url.openStream();
    File tmpSigFile = File.createTempFile("tmpsigfile", ".xml");
    FileOutputStream fos = new FileOutputStream(tmpSigFile);
    IOUtils.copy(sigFileStream, fos);
    fos.close();
    dihj = DroidIdentification.getInstance(tmpSigFile.getAbsolutePath());
}

From source file:eu.scape_project.pc.droid.DroidIdentificationTaskTest.java

/**
 * Set up.//ww w . j  a v a 2  s .com
 * @throws Exception 
 */
@BeforeClass
public static void setUpClass() throws Exception {
    URL sigFileV67Url = new URL(SIGNATURE_FILE_V67_URL);
    InputStream sigFileStream = sigFileV67Url.openStream();
    File tmpSigFile = File.createTempFile("tmpsigfile", ".xml");
    FileOutputStream fos = new FileOutputStream(tmpSigFile);
    IOUtils.copy(sigFileStream, fos);
    fos.close();
    dihj = DroidIdentificationTask.getInstance(tmpSigFile.getAbsolutePath());
}

From source file:com.jlgranda.fede.ejb.url.reader.FacturaElectronicaURLReader.java

/**
 * Obtiene la lista de objetos factura para el sujeto en fede
 *
 * @param urls URLs hacia los archivo XML o ZIP a leer
 * @return una lista de instancias FacturaReader
 *///from   ww w  . j  a  v a  2s . c  o m
public static FacturaReader getFacturaElectronica(String url) throws Exception {
    FacturaReader facturaReader = null;

    if (url.endsWith(".xml")) {
        String xml = FacturaElectronicaURLReader.read(url);
        facturaReader = new FacturaReader(FacturaUtil.read(xml), xml, url);
    } else if (url.endsWith(".zip")) {
        URL url_ = new URL(url);
        InputStream is = url_.openStream();
        ZipInputStream zis = new ZipInputStream(is);
        try {

            ZipEntry entry = null;
            String tmp = null;
            ByteArrayOutputStream fout = null;
            while ((entry = zis.getNextEntry()) != null) {
                if (entry.getName().toLowerCase().endsWith(".xml")) {
                    fout = new ByteArrayOutputStream();
                    for (int c = zis.read(); c != -1; c = zis.read()) {
                        fout.write(c);
                    }

                    tmp = new String(fout.toByteArray(), Charset.defaultCharset());
                    facturaReader = new FacturaReader(FacturaUtil.read(tmp), tmp, url);
                    fout.close();
                }
                zis.closeEntry();
            }
            zis.close();

        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(zis);
        }
    }

    return facturaReader;
}