List of usage examples for java.net URL openStream
public final InputStream openStream() throws java.io.IOException
From source file:Main.java
public static String readFile(String link) { try {//from w w w. ja va 2s.c om // Create a URL for the desired page URL url = new URL(link); // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String text = ""; String str; while ((str = in.readLine()) != null) text += str + "\n"; in.close(); return text; } catch (MalformedURLException e) { e.printStackTrace(); return e.getMessage(); } catch (IOException e) { e.printStackTrace(); return e.getMessage(); } }
From source file:Main.java
public static Element loadDocument(String value, String type) { Document doc = null;/* w ww . j ava2s. c om*/ InputSource xmlInp = null; try { if (type.equals("location")) { URL url = new URL(value); xmlInp = new InputSource(url.openStream()); } else { xmlInp = new InputSource(new StringReader(value)); } DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = docBuilderFactory.newDocumentBuilder(); doc = parser.parse(xmlInp); Element root = doc.getDocumentElement(); root.normalize(); return root; } catch (SAXParseException err) { System.err.println("URLMappingsXmlDAO ** Parsing error, line " + err.getLineNumber() + ", uri " + err.getSystemId()); System.err.println("URLMappingsXmlDAO error: " + err.getMessage()); } catch (SAXException e) { System.err.println("URLMappingsXmlDAO error: " + e); } catch (MalformedURLException mfx) { System.err.println("URLMappingsXmlDAO error: " + mfx); } catch (IOException e) { System.err.println("URLMappingsXmlDAO error: " + e); } catch (Exception pce) { System.err.println("URLMappingsXmlDAO error: " + pce); } return null; }
From source file:com.dicksoft.ocr.util.HttpUtil.java
/** * Downloads the text from the specified URL. * //from w w w .j av a2 s.c om * @param url * the URL of the text to read * @return the text * @throws IOException * if the URL is malformed, or problem reading the stream */ public static String fetchText(String url) throws IOException { if (LOG.isDebugEnabled()) LOG.debug("Http fetch: " + url); StringBuffer result = new StringBuffer(); URL urlReal = null; urlReal = new URL(url); BufferedInputStream in = null; in = new BufferedInputStream(urlReal.openStream()); int data = 0; while (true) { data = in.read(); if (data == -1) break; else result.append((char) data); } return result.toString(); }
From source file:net.erdfelt.android.sdkfido.logging.Logging.java
public static void config() { ClassLoader cl = Thread.currentThread().getContextClassLoader(); URL url = cl.getResource("logging.properties"); if (url != null) { InputStream in = null;/*from w w w . j a v a2s.c om*/ try { in = url.openStream(); LogManager.getLogManager().readConfiguration(in); } catch (IOException e) { e.printStackTrace(System.err); } finally { IOUtils.closeQuietly(in); } } System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger"); }
From source file:fr.mael.microrss.util.Tools.java
/** * Reads an image from an url and converts it to byte array * @param url//from w ww . j a v a2s . c o m * @return a byte array representing the image */ public static byte[] getImage(String url) { try { URL imgUrl = new URL(url); InputStream inputStream = imgUrl.openStream(); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int n = 0; while (-1 != (n = inputStream.read(buffer))) { output.write(buffer, 0, n); } inputStream.close(); byte[] data = output.toByteArray(); return data; } catch (Exception e) { LOG.info("Error get image " + url, e); } return null; }
From source file:Main.java
public static Element loadDocument(String location) { Document doc = null;// w ww . jav a 2 s . c om try { URL url = new URL(location); InputSource xmlInp = new InputSource(url.openStream()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = docBuilderFactory.newDocumentBuilder(); doc = parser.parse(xmlInp); Element root = doc.getDocumentElement(); root.normalize(); return root; } catch (SAXParseException err) { System.err.println("URLMappingsXmlDAO ** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId()); System.err.println("URLMappingsXmlDAO error: " + err.getMessage()); } catch (SAXException e) { System.err.println("URLMappingsXmlDAO error: " + e); } catch (java.net.MalformedURLException mfx) { System.err.println("URLMappingsXmlDAO error: " + mfx); } catch (java.io.IOException e) { System.err.println("URLMappingsXmlDAO error: " + e); } catch (Exception pce) { System.err.println("URLMappingsXmlDAO error: " + pce); } return null; }
From source file:com.newrelic.agent.deps.org.apache.http.conn.util.PublicSuffixMatcherLoader.java
public static PublicSuffixMatcher load(final URL url) throws IOException { Args.notNull(url, "URL"); final InputStream in = url.openStream(); try {/*from w w w.j a va 2 s . com*/ return load(in); } finally { in.close(); } }
From source file:Main.java
public static boolean WriteFileFromUrl(String url, String filename) { try {//from w w w .j a v a 2s . co m URL fileUrl = new URL(url); URLConnection connection = fileUrl.openConnection(); InputStream inputStream = new BufferedInputStream(fileUrl.openStream(), 10240); File cacheFile = new File(filename); FileOutputStream outputStream = new FileOutputStream(cacheFile); byte buffer[] = new byte[1024]; int dataSize; int loadedSize = 0; while ((dataSize = inputStream.read(buffer)) != -1) { loadedSize += dataSize; outputStream.write(buffer, 0, dataSize); } outputStream.close(); return true; } catch (Exception e) { Log.d("#StorageHelper Error:", e.toString()); return false; } }
From source file:javarestart.Utils.java
static File fetchResourceToTempFile(String resName, String resExt, URL from) { try {/*w w w. j ava2s . c om*/ return fetchResourceToTempFile(resName, resExt, from.openStream()); } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:StringIO.java
/** * Read the entire contents of the specified URL and return a * single String object containing the contents of the URL. * This method does not return until an end of stream is reached * for the URL./* w ww. ja v a2s . com*/ * * @param url a URL from which to read * * @return a String containing the contents of the input URL * * @throws IOException if the specified URL cannot be opened, or * if an I/O error occurs while reading the URL */ public static String readFully(URL url) throws IOException { return readFully(url.openStream()); }