List of usage examples for java.net URL openStream
public final InputStream openStream() throws java.io.IOException
From source file:Main.java
private static void downloadResource(String from, String to) { OutputStream outputStream = null; BufferedInputStream inputStream = null; HttpURLConnection connection = null; URL url; byte[] buffer = new byte[1024]; try {//from w w w . j a va 2s. co m url = new URL(from); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.connect(); outputStream = new FileOutputStream(to); inputStream = new BufferedInputStream(url.openStream()); int read; while ((read = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, read); } } catch (IOException e) { e.printStackTrace(); } finally { if (outputStream != null) try { outputStream.close(); } catch (IOException e) { } if (inputStream != null) try { inputStream.close(); } catch (IOException e) { } if (connection != null) connection.disconnect(); } }
From source file:de.nrw.hbz.deepzoomer.fileUtil.FileUtil.java
/** * <p><em>Title: Create a temporary File from a file identified by URL</em></p> * <p>Description: Method creates a temporary file from a remote file addressed * an by URL representing the orginal PDF, that should be converted</p> * // www . ja va2s .co m * @param fileName * @param url * @return */ public static String saveUrlToFile(String fileName, String url) { File inputFile = new File(Configuration.getTempDirPath() + "/" + fileName); log.debug(inputFile.getAbsolutePath()); InputStream is = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; FileOutputStream fos = null; log.info(url); try { URL inputDocument = new URL(url); is = inputDocument.openStream(); bis = new BufferedInputStream(is); fos = new FileOutputStream(inputFile); bos = new BufferedOutputStream(fos); int i = -1; while ((i = bis.read()) != -1) { bos.write(i); } bos.flush(); } catch (Exception e) { log.error(e); } finally { if (bos != null) { try { bos.close(); } catch (IOException ioExc) { log.error(ioExc); } } if (fos != null) { try { fos.close(); } catch (IOException ioExc) { log.error(ioExc); } } if (bis != null) { try { bis.close(); } catch (IOException ioExc) { log.error(ioExc); } } if (is != null) { try { is.close(); } catch (IOException ioExc) { log.error(ioExc); } } } return inputFile.getName(); }
From source file:hsyndicate.utils.IPUtils.java
public static String getPublicIPAddress() { if (cachedPublicIP != null && !cachedPublicIP.isEmpty()) { return cachedPublicIP; } else {/*from w ww .j a v a2 s . c o m*/ try { URL whatismyip = new URL("http://checkip.amazonaws.com"); BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(whatismyip.openStream())); String ip = in.readLine(); // cache cachedPublicIP = ip; return ip; } catch (IOException ex) { LOG.error("Exception occurred while querying public ip address to amazonaws.com", ex); } finally { if (in != null) { try { in.close(); } catch (IOException e) { LOG.error("Exception occurred while querying public ip address to amazonaws.com", e); } } } } catch (MalformedURLException ex) { LOG.error("Exception occurred while querying public ip address to amazonaws.com", ex); } } return null; }
From source file:net.sf.ehcache.config.ConfigurationFactory.java
/** * Configures a bean from an XML file available as an URL. *//*w w w. j a v a2 s. c o m*/ public static Configuration parseConfiguration(final URL url) throws CacheException { if (LOG.isDebugEnabled()) { LOG.debug("Configuring ehcache from URL: " + url); } Configuration configuration; InputStream input = null; try { input = url.openStream(); configuration = parseConfiguration(input); } catch (Exception e) { throw new CacheException("Error configuring from " + url + ". Initial cause was " + e.getMessage(), e); } finally { try { if (input != null) { input.close(); } } catch (IOException e) { LOG.error("IOException while closing configuration input stream. Error was " + e.getMessage()); } } return configuration; }
From source file:com.feilong.core.lang.ClassLoaderUtil.java
/** * This is a convenience method to load a resource as a stream. * //from w ww. jav a2 s. c o m * <h3>?:</h3> * <blockquote> * <ol> * <li> <code>resourceName</code> ? "/" ,?, ClassLoader???? ?, ?? * <code>org.springframework.core.io.ClassPathResource#ClassPathResource(String, ClassLoader)</code></li> * <li>"",classes </li> * </ol> * </blockquote> * * @param resourceName * The name of the resource to load * @param callingClass * The Class object of the calling object * @return <code>resourceName</code> null, {@link NullPointerException}<br> * ??, null * @see #getResourceInAllClassLoader(String, Class) * @see "org.apache.velocity.util.ClassUtils#getResourceAsStream(Class, String)" */ public static InputStream getResourceAsStream(String resourceName, Class<?> callingClass) { URL url = getResourceInAllClassLoader(resourceName, callingClass); try { return url == null ? null : url.openStream(); } catch (IOException e) { String message = Slf4jUtil.format("can not open resourceName:[{}]", resourceName); LOGGER.error(message, e); throw new UncheckedIOException(message, e); } }
From source file:com.sunchenbin.store.feilong.core.lang.ClassLoaderUtil.java
/** * This is a convenience method to load a resource as a stream. <br> * The algorithm used to find the resource is given in getResource() * /*from w ww . j av a 2 s. co m*/ * @param resourceName * The name of the resource to load * @param callingClass * The Class object of the calling object * @return the resource as stream * @see #getResource(String, Class) * @see "org.apache.velocity.util.ClassUtils#getResourceAsStream(Class, String)" */ public static InputStream getResourceAsStream(String resourceName, Class<?> callingClass) { URL url = getResource(resourceName, callingClass); try { return (url != null) ? url.openStream() : null; } catch (IOException e) { throw new UncheckedIOException(e); } }
From source file:jcurl.core.io.SetupSaxDeSer.java
public static SetupBuilder parse(final URL file) throws SAXException, IOException { if (file.getFile().endsWith("z")) return parse(new GZIPInputStream(file.openStream())); return parse(file.openStream()); }
From source file:com.intuit.tank.tools.debugger.PanelBuilder.java
private static Headers getHeaders(String serviceUrl) { Headers ret = null;//from w w w .j ava2s.c o m if (StringUtils.isNotBlank(serviceUrl)) { InputStream settingsStream = null; try { URL url = new URL(serviceUrl + HEADERS_PATH); LOG.info( "Starting up: making call to tank service url to get settings.xml " + url.toExternalForm()); settingsStream = url.openStream(); JAXBContext ctx = JAXBContext.newInstance(Headers.class.getPackage().getName()); ret = (Headers) ctx.createUnmarshaller().unmarshal(settingsStream); } catch (Exception e) { LOG.error("Error gettting headers: " + e, e); } finally { IOUtils.closeQuietly(settingsStream); } } return ret; }
From source file:Main.java
public static String readTextFromURL(String urlString) throws IOException { HttpURLConnection urlConnection = null; URL url = new URL(urlString); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setReadTimeout(10000 /* milliseconds */); urlConnection.setConnectTimeout(15000 /* milliseconds */); urlConnection.setDoOutput(true);// w ww .j a v a 2 s . c om urlConnection.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); char[] buffer = new char[1024]; String jsonString = new String(); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); return sb.toString(); }
From source file:guiTool.Helper.java
public static String readUrl(String urlString, final String userid, final String password) throws Exception { BufferedReader reader = null; StringBuilder buffer = null;//from w w w . ja v a 2 s . c om try { URL url = new URL(urlString); Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userid, password.toCharArray()); } }); InputStreamReader s = new InputStreamReader(url.openStream()); // System.out.println("getEncoding " + s.getEncoding()); reader = new BufferedReader(s); buffer = new StringBuilder(); int read; char[] chars = new char[1024]; while ((read = reader.read(chars)) != -1) { buffer.append(chars, 0, read); } } catch (Exception e) { System.out.println("Exception " + e.getMessage()); // JOptionPane.showMessageDialog(null, "The server or your internet connection could be down.", "connection test", JOptionPane.ERROR_MESSAGE); } finally { if (reader != null) { reader.close(); } } return buffer.toString(); }