List of usage examples for java.net URL openStream
public final InputStream openStream() throws java.io.IOException
From source file:com.mebigfatguy.polycasso.URLFetcher.java
/** * retrieve arbitrary data found at a specific url * - either http or file urls/*w w w. ja v a2s. c o m*/ * for http requests, sets the user-agent to mozilla to avoid * sites being cranky about a java sniffer * * @param url the url to retrieve * @param proxyHost the host to use for the proxy * @param proxyPort the port to use for the proxy * @return a byte array of the content * * @throws IOException the site fails to respond */ public static byte[] fetchURLData(String url, String proxyHost, int proxyPort) throws IOException { HttpURLConnection con = null; InputStream is = null; try { URL u = new URL(url); if (url.startsWith("file://")) { is = new BufferedInputStream(u.openStream()); } else { Proxy proxy; if (proxyHost != null) { proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); } else { proxy = Proxy.NO_PROXY; } con = (HttpURLConnection) u.openConnection(proxy); con.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6"); con.addRequestProperty("Accept-Charset", "UTF-8"); con.addRequestProperty("Accept-Language", "en-US,en"); con.addRequestProperty("Accept", "text/html,image/*"); con.setDoInput(true); con.setDoOutput(false); con.connect(); is = new BufferedInputStream(con.getInputStream()); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(is, baos); return baos.toByteArray(); } finally { IOUtils.closeQuietly(is); if (con != null) { con.disconnect(); } } }
From source file:org.n52.iceland.util.JSONUtils.java
public static JsonNode loadURL(final URL url) throws IOException { return getReader().readTree(url.openStream()); }
From source file:com.github.pemapmodder.pocketminegui.utils.Utils.java
private static File installWindowsPHP(File home, InstallProgressReporter progress) { progress.report(0.0);//from w ww . ja v a2 s . co m try { String link = System.getProperty("os.arch").contains("64") ? "https://bintray.com/artifact/download/pocketmine/PocketMine/PHP_7.0.3_x64_Windows.tar.gz" : "https://bintray.com/artifact/download/pocketmine/PocketMine/PHP_7.0.3_x86_Windows.tar.gz"; URL url = new URL(link); InputStream gz = url.openStream(); GzipCompressorInputStream tar = new GzipCompressorInputStream(gz); TarArchiveInputStream is = new TarArchiveInputStream(tar); TarArchiveEntry entry; while ((entry = is.getNextTarEntry()) != null) { if (!entry.isDirectory()) { String name = entry.getName(); byte[] buffer = new byte[(int) entry.getSize()]; IOUtils.read(is, buffer); File real = new File(home, name); real.getParentFile().mkdirs(); IOUtils.write(buffer, new FileOutputStream(real)); } } File output = new File(home, "bin/php/php.exe"); progress.completed(output); return output; } catch (IOException e) { e.printStackTrace(); progress.errored(); return null; } }
From source file:com.aol.advertising.qiao.util.XmlConfigUtil.java
/** * Validate an XML document against the given schema. * * @param xmlFileLocationUri/*from w ww . ja v a2 s.c om*/ * Location URI of the document to be validated. * @param schemaLocationUri * Location URI of the XML schema in W3C XML Schema Language. * @return true if valid, false otherwise. */ public static boolean validateXml(String xmlFileLocationUri, String schemaLocationUri) { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); InputStream is = null; try { Schema schema = factory.newSchema(CommonUtils.uriToURL(schemaLocationUri)); Validator validator = schema.newValidator(); URL url = CommonUtils.uriToURL(xmlFileLocationUri); is = url.openStream(); Source source = new StreamSource(is); validator.validate(source); logger.info(">> successfully validated configuration file: " + xmlFileLocationUri); return true; } catch (SAXParseException e) { logger.error(e.getMessage() + " (line:" + e.getLineNumber() + ", col:" + e.getColumnNumber() + ")"); } catch (Exception e) { if (e instanceof SAXParseException) { SAXParseException e2 = (SAXParseException) e; logger.error(e2.getMessage() + "at line:" + e2.getLineNumber() + ", col:" + e2.getColumnNumber()); } else { logger.error(e.getMessage()); } } finally { IOUtils.closeQuietly(is); } return false; }
From source file:com.github.fge.jackson.JsonLoader.java
/** * Read a {@link JsonNode} from an URL./* ww w.jav a2s. com*/ * * @param url The URL to fetch the JSON document from * @return The document at that URL * @throws IOException in case of network problems etc. */ public static JsonNode fromURL(final URL url) throws IOException { return READER.fromInputStream(url.openStream()); }
From source file:com.wxxr.nirvana.json.TestUtils.java
public static String readContent(URL url) throws Exception { if (url == null) throw new Exception("unable to verify a null URL"); return IOUtils.toString(url.openStream()); }
From source file:Main.java
/** * open instream for a XML file from URL * * @param the/*from ww w .jav a 2 s. c o m*/ * source url * @return the DOM document */ public static Document openXMLStream(URL url) { try { // Create a builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); InputStream stream = url.openStream(); // Create the builder and parse the stream Document doc = factory.newDocumentBuilder().parse(stream); return doc; } catch (SAXException e) { // A parsing error occurred; the xml input is not valid } catch (ParserConfigurationException ex) { System.out.println(ex.getMessage()); } catch (IOException ex) { System.out.println(ex.getMessage()); } return null; }
From source file:ch.algotrader.esper.EsperTestBase.java
protected static Module load(final URL resource, final String name) throws IOException, ParseException { try (Reader reader = new InputStreamReader(resource.openStream(), Charsets.ISO_8859_1)) { return load(reader, name); }/* w ww. ja v a2 s . c om*/ }
From source file:net.sf.jabref.exporter.OpenOfficeDocumentCreator.java
private static void addFromResource(String resource, OutputStream out) { URL url = OpenOfficeDocumentCreator.class.getResource(resource); try (InputStream in = url.openStream()) { byte[] buffer = new byte[256]; synchronized (out) { while (true) { int bytesRead = in.read(buffer); if (bytesRead == -1) { break; }// w w w . j ava 2s. c om out.write(buffer, 0, bytesRead); } } } catch (IOException e) { LOGGER.warn("Cannot get resource", e); } }
From source file:GetURL.java
/** * This method will display the URL specified by the parameter. * /*from w w w . j av a 2 s .c o m*/ * @param u * The URL to display. */ static protected void getURL(String u) { URL url; InputStream is; InputStreamReader isr; BufferedReader r; String str; try { System.out.println("Reading URL: " + u); url = new URL(u); is = url.openStream(); isr = new InputStreamReader(is); r = new BufferedReader(isr); do { str = r.readLine(); if (str != null) System.out.println(str); } while (str != null); } catch (MalformedURLException e) { System.out.println("Must enter a valid URL"); } catch (IOException e) { System.out.println("Can not connect"); } }