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

/**
 * Saves a file from the given URL to the given filename and returns the file
 * @param link URL to file/* w w w.jav a 2 s .  co  m*/
 * @param fileName Name to save the file
 * @return The file
 * @throws IOException Thrown if any IOException occurs
 */
public static File saveFileFromNet(URL link, String fileName) throws IOException {
    InputStream in = new BufferedInputStream(link.openStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int n = 0;
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();
    byte[] response = out.toByteArray();

    File file = new File(fileName);
    if (!file.exists()) {
        if (file.getParentFile() != null) {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
    }
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(response);
    fos.close();

    return new File(fileName);
}

From source file:com.aestasit.markdown.Markdown.java

public static RootNode toAst(final URL url) throws IOException {
    return toAst(url.openStream());
}

From source file:edu.ucla.cs.scai.swim.qa.ontology.dbpedia.tipicality.DbpediaCsvDownload.java

private static void download(Element e) throws MalformedURLException, IOException {
    for (Element c : e.children()) {
        String tagName = c.tag().getName();
        if (tagName.equals("small")) {
            for (Element c1 : c.children()) {
                if (c1.tag().getName().equals("a") && c1.text().equalsIgnoreCase("csv")) {
                    String href = c1.attr("href");
                    System.out.println("Downloading " + href);
                    try {
                        URL remoteFile = new URL(href);
                        ReadableByteChannel rbc = Channels.newChannel(remoteFile.openStream());
                        String[] s = href.split("\\/");
                        FileOutputStream fos = new FileOutputStream(
                                DBpediaOntology.DBPEDIA_CSV_FOLDER + s[s.length - 1]);
                        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }// w w w.ja v  a  2  s . c om
                }
            }
        } else if (tagName.equals("ul")) {
            for (Element c1 : c.children()) {
                if (c1.tagName().equals("li")) {
                    download(c1);
                }
            }
        }
    }
}

From source file:com.frameworkset.common.tag.export.properties.ConfigFactory.java

/**
 * ?xml???//from  w  w w .  j a va2  s  . c  o  m
 * @param force
 * @return Configture
 */
public static Configture createConfigure(boolean force) {
    Digester digester = new Digester();
    digester.setValidating(false);
    if (configture == null || force) {
        configture = new Configture();

        digester.push(configture);
        // This set of rules calls the addDataSource method and passes
        // in five parameters to the method.
        digester.addBeanPropertySetter("exportconfig/description", "description");

        digester.addBeanPropertySetter("exportconfig/version", "version");
        digester.addObjectCreate("exportconfig/export", ExportConfig.class);
        digester.addBeanPropertySetter("exportconfig/export/description", "description");
        digester.addSetProperties("exportconfig/export/config", new String[] { "name", "type", "class" },
                new String[] { "name", "type", "exportClass" });
        digester.addSetNext("exportconfig/export", "addExportConfig");
        ClassLoader classLoader = ConfigFactory.class.getClassLoader();
        URL url = classLoader.getResource("export.xml");
        try {
            digester.parse(url.openStream());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {

            e.printStackTrace();
        }

    }
    return configture;
}

From source file:GoogleImages.java

public static void saveImage(String imageUrl, String destinationFile) throws IOException {
    URL url = new URL(imageUrl);
    InputStream is = url.openStream();
    OutputStream os = new FileOutputStream(destinationFile);

    byte[] b = new byte[2048];
    int length;//from w w w . jav  a 2  s .com

    while ((length = is.read(b)) != -1) {
        os.write(b, 0, length);
    }

    is.close();
    os.close();
}

From source file:com.github.walterfan.util.http.URLHelper.java

public static void URL2Stream(String strUrl, OutputStream os) throws IOException {
    InputStream input = null;//from   ww w.  j  a va2 s  . c om
    try {
        URL url = new URL(strUrl);
        input = url.openStream();
        IOUtils.copy(input, os);
    } finally {
        IOUtils.closeQuietly(input);
    }
}

From source file:Main.java

private static InputSource toInputSource(URL xmlFile) {
    InputSource iso;//www .ja v a2s .c o  m
    try {
        iso = new InputSource(xmlFile.openStream());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return iso;
}

From source file:Main.java

/** Loads properties into a file.
 * <P>This assumes the file was written with the
 * <code>Properties.store()</code> method.
 *///from w  w w  .  j  ava 2 s .c  o m
public static void load(Properties p, URL url) throws IOException {
    InputStream in = null;
    try {
        in = url.openStream();
        p.load(in);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Throwable t) {
            }
        }
    }
}

From source file:Main.java

/**
 * @param xmlURL is a URL to an XML document
 * @return the Document contained in the content at tha tURL
 *///from   w ww  .  j a  va 2s .co m
public static Document readURL(String xmlURL) {
    ensureFactory();
    try {
        URL u = new URL(xmlURL);
        DocumentBuilder builder = mFactory.newDocumentBuilder();
        return builder.parse(u.openStream());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.indoqa.maven.wadldoc.transformation.Wadl2HtmlPipelineTest.java

private static Diff createDiff(URL expected, ByteArrayOutputStream actual) throws Exception {
    String string1 = IOUtils.toString(expected.openStream());
    String string2 = actual.toString();

    return new Diff(string1, string2);
}