List of usage examples for java.net URLConnection connect
public abstract void connect() throws IOException;
From source file:org.apache.ignite.testsuites.IgniteHadoopTestSuite.java
/** * Downloads and extracts an Apache product. * * @param appName Name of application for log messages. * @param homeVariable Pointer to home directory of the component. * @param downloadPath Relative download path of tar package. * @param destName Local directory name to install component. * @throws Exception If failed.//from w w w . ja va 2 s .com */ private static void download(String appName, String homeVariable, String downloadPath, String destName) throws Exception { String homeVal = IgniteSystemProperties.getString(homeVariable); if (!F.isEmpty(homeVal) && new File(homeVal).isDirectory()) { X.println(homeVariable + " is set to: " + homeVal); return; } List<String> urls = F.asList("http://archive.apache.org/dist/", "http://apache-mirror.rbc.ru/pub/apache/", "http://www.eu.apache.org/dist/", "http://www.us.apache.org/dist/"); String tmpPath = System.getProperty("java.io.tmpdir"); X.println("tmp: " + tmpPath); final File install = new File(tmpPath + File.separatorChar + "__hadoop"); final File home = new File(install, destName); X.println("Setting " + homeVariable + " to " + home.getAbsolutePath()); System.setProperty(homeVariable, home.getAbsolutePath()); final File successFile = new File(home, "__success"); if (home.exists()) { if (successFile.exists()) { X.println(appName + " distribution already exists."); return; } X.println(appName + " distribution is invalid and it will be deleted."); if (!U.delete(home)) throw new IOException("Failed to delete directory: " + home.getAbsolutePath()); } for (String url : urls) { if (!(install.exists() || install.mkdirs())) throw new IOException("Failed to create directory: " + install.getAbsolutePath()); URL u = new URL(url + downloadPath); X.println("Attempting to download from: " + u); try { URLConnection c = u.openConnection(); c.connect(); try (TarArchiveInputStream in = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(c.getInputStream(), 32 * 1024)))) { TarArchiveEntry entry; while ((entry = in.getNextTarEntry()) != null) { File dest = new File(install, entry.getName()); if (entry.isDirectory()) { if (!dest.mkdirs()) throw new IllegalStateException(); } else if (entry.isSymbolicLink()) { // Important: in Hadoop installation there are symlinks, we need to create them: Path theLinkItself = Paths.get(install.getAbsolutePath(), entry.getName()); Path linkTarget = Paths.get(entry.getLinkName()); Files.createSymbolicLink(theLinkItself, linkTarget); } else { File parent = dest.getParentFile(); if (!(parent.exists() || parent.mkdirs())) throw new IllegalStateException(); X.print(" [" + dest); try (BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(dest, false), 128 * 1024)) { U.copy(in, out); out.flush(); } Files.setPosixFilePermissions(dest.toPath(), modeToPermissionSet(entry.getMode())); X.println("]"); } } } if (successFile.createNewFile()) return; } catch (Exception e) { e.printStackTrace(); U.delete(home); } } throw new IllegalStateException("Failed to install " + appName + "."); }
From source file:com.comcast.cdn.traffic_control.traffic_monitor.util.Fetcher.java
public static String fetchContent(final URLConnection conn, final int timeout) throws IOException { conn.setAllowUserInteraction(true);//from w w w.j a va 2 s. c om if (timeout != 0) { conn.setConnectTimeout(timeout); conn.setReadTimeout(timeout); } conn.connect(); return IOUtils.toString(new InputStreamReader(conn.getInputStream(), UTF8_STR)); }
From source file:org.cgiar.ilri.odk.pull.backend.DataHandler.java
/** * Checks whether the server responds before the timeout. * * @param timeout The timeout in milliseconds * * @return TRUE if the server responds before the timeout *//*from ww w .j a v a2 s. c o m*/ private static boolean isConnectedToServer(int timeout) { try { URL myUrl = new URL("http://azizi.ilri.cgiar.org"); URLConnection connection = myUrl.openConnection(); connection.setConnectTimeout(timeout); connection.connect(); return true; } catch (Exception e) { // Handle your exceptions return false; } }
From source file:com.gh4a.utils.ImageDownloader.java
/** * Download bitmap.//from w ww. j av a 2 s.co m * * @param urlStr the url str * @return the bitmap */ static Bitmap downloadBitmap(String urlStr) { InputStream is = null; try { URL url = new URL(urlStr); URLConnection conn = url.openConnection(); conn.connect(); is = conn.getInputStream(); // bis = new BufferedInputStream(is); final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(is)); return bitmap; } catch (Exception e) { Log.e(Constants.LOG_TAG, e.getMessage(), e); } finally { if (is != null) { try { is.close(); } catch (IOException e) { Log.e(Constants.LOG_TAG, e.getMessage(), e); } } } return null; }
From source file:org.apache.niolex.commons.net.DownloadUtil.java
/** * Download the file pointed by the url and return the content as byte array. * * @param strUrl/*from ww w .j a va 2 s . c o m*/ * The Url to be downloaded. * @param connectTimeout * Connect timeout in milliseconds. * @param readTimeout * Read timeout in milliseconds. * @param maxFileSize * Max file size in BYTE. * @param useCache Whether we use thread local cache or not. * @return The file content as byte array. * @throws NetException */ public static byte[] downloadFile(String strUrl, int connectTimeout, int readTimeout, int maxFileSize, Boolean useCache) throws NetException { LOG.debug("Start to download file [{}], C{}R{}M{}.", strUrl, connectTimeout, readTimeout, maxFileSize); InputStream in = null; try { URL url = new URL(strUrl); // We use Java URL to download file. URLConnection ucon = url.openConnection(); ucon.setConnectTimeout(connectTimeout); ucon.setReadTimeout(readTimeout); ucon.setDoOutput(false); ucon.setDoInput(true); ucon.connect(); final int contentLength = ucon.getContentLength(); validateContentLength(strUrl, contentLength, maxFileSize); if (ucon instanceof HttpURLConnection) { validateHttpCode(strUrl, (HttpURLConnection) ucon); } in = ucon.getInputStream(); // Get the input stream. byte[] ret = null; // Create the byte array buffer according to the strategy. if (contentLength > 0) { ret = commonDownload(contentLength, in); } else { ret = unusualDownload(strUrl, in, maxFileSize, useCache); } LOG.debug("Succeeded to download file [{}] size {}.", strUrl, ret.length); return ret; } catch (NetException e) { LOG.info(e.getMessage()); throw e; } catch (Exception e) { String msg = "Failed to download file " + strUrl + " msg=" + e.toString(); LOG.warn(msg); throw new NetException(NetException.ExCode.IOEXCEPTION, msg, e); } finally { // Close the input stream. StreamUtil.closeStream(in); } }
From source file:com.cw.litenote.util.video.UtilVideo.java
public static String getVideoDataSource(String path) throws IOException { if (!URLUtil.isNetworkUrl(path)) { return path; } else {// w ww . jav a2 s . com URL url = new URL(path); URLConnection cn = url.openConnection(); cn.connect(); InputStream stream = cn.getInputStream(); if (stream == null) throw new RuntimeException("stream is null"); File temp = File.createTempFile("mediaplayertmp", "dat"); temp.deleteOnExit(); String tempPath = temp.getAbsolutePath(); FileOutputStream out = new FileOutputStream(temp); byte buf[] = new byte[128]; do { System.setProperty("http.keepAlive", "false");//??? need this? int numread = stream.read(buf); if (numread <= 0) break; out.write(buf, 0, numread); } while (true); try { stream.close(); out.close(); } catch (IOException ex) { Log.e(TAG_VIDEO, "error: " + ex.getMessage(), ex); } System.out.println("UtilVideo / _getVideoDataSource / tempPath " + tempPath); return tempPath; } }
From source file:com.comcast.cdn.traffic_control.traffic_monitor.util.Fetcher.java
public static String fetchContent(final String stateUrl, final String ipStr, final int port, final int timeout) throws IOException { final URLConnection conn = new URL(stateUrl) .openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipStr, port))); if (timeout != 0) { conn.setConnectTimeout(timeout); conn.setReadTimeout(timeout);//from w w w . ja v a2 s.com } conn.connect(); return IOUtils.toString(new InputStreamReader(conn.getInputStream(), UTF8_STR)); }
From source file:finale.year.stage.utility.Util.java
public static boolean isAv(JFrame frame) { //Try to access the server try {//w w w . jav a 2 s . c o m final URL url = new URL(ROOT); final URLConnection con = url.openConnection(); con.connect(); } catch (MalformedURLException e) { JOptionPane.showMessageDialog(frame, "Check Internet Connection"); } catch (IOException ex) { JOptionPane.showMessageDialog(frame, "Check Internet Connection"); return false; } return true; }
From source file:com.feathercoin.wallet.feathercoin.ExchangeRatesProvider.java
private static Map<String, ExchangeRate> getFeathercoinCharts() { final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); // Keep the BTC rate around for a bit Double btcRate = 0.0;/*from www . j ava2s . c om*/ try { /* String currencies[] = {"USD", "BTC", "RUR"}; String urls[] = {"https://btc-e.com/api/2/ftc_btc/ticker", "https://btc-e.com/api/2/10/ticker", "https://btc-e.com/api/2/ltc_rur/ticker"}; for(int i = 0; i < currencies.length; ++i) { final String currencyCode = currencies[i]; final URL URL = new URL(urls[i]); final URLConnection connection = URL.openConnection(); connection.setConnectTimeout(TIMEOUT_MS); connection.setReadTimeout(TIMEOUT_MS); connection.connect(); final StringBuilder content = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024)); IOUtils.copy(reader, content); final JSONObject head = new JSONObject(content.toString()); JSONObject ticker = head.getJSONObject("ticker"); Double avg = ticker.getDouble("avg"); String euros = String.format("%.4f", avg); // Fix things like 3,1250 euros = euros.replace(",", "."); rates.put(currencyCode, new ExchangeRate(currencyCode, Utils.toNanoCoins(euros), URL.getHost())); if(currencyCode.equalsIgnoreCase("BTC")) btcRate = avg; } finally { if (reader != null) reader.close(); } }*/ // Handle FTC/USD special since we have to do maths URL URL = null; try { URL = new URL("https://btc-e.com/api/2/ftc_btc/ticker"); } catch (MalformedURLException e) { e.printStackTrace(); } URLConnection connection = URL.openConnection(); connection.setConnectTimeout(TIMEOUT_MS); connection.setReadTimeout(TIMEOUT_MS); connection.connect(); StringBuilder content = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024)); IOUtils.copy(reader, content); final JSONObject head = new JSONObject(content.toString()); JSONObject ticker = head.getJSONObject("ticker"); Double avg = ticker.getDouble("avg"); // This is feathercoins priced in bitcoins btcRate = avg; String s_avg = String.format("%.4f", avg).replace(',', '.'); rates.put("BTC", new ExchangeRate("BTC", Utils.toNanoCoins(s_avg), URL.getHost())); } finally { if (reader != null) reader.close(); } // Handle FTC/USD special since we have to do maths URL = null; URL = new URL("https://btc-e.com/api/2/btc_usd/ticker"); connection = URL.openConnection(); connection.setConnectTimeout(TIMEOUT_MS); connection.setReadTimeout(TIMEOUT_MS); connection.connect(); content = new StringBuilder(); reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024)); IOUtils.copy(reader, content); final JSONObject head = new JSONObject(content.toString()); JSONObject ticker = head.getJSONObject("ticker"); Double avg = ticker.getDouble("avg"); // This is bitcoins priced in dollars. We want FTC! avg *= btcRate; String s_avg = String.format("%.4f", avg).replace(',', '.'); rates.put("USD", new ExchangeRate("USD", Utils.toNanoCoins(s_avg), URL.getHost())); } finally { if (reader != null) reader.close(); } // Handle FTC/BTC special since we have to do maths URL = new URL("https://btc-e.com/api/2/btc_eur/ticker"); connection = URL.openConnection(); connection.setConnectTimeout(TIMEOUT_MS); connection.setReadTimeout(TIMEOUT_MS); connection.connect(); content = new StringBuilder(); reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024)); IOUtils.copy(reader, content); final JSONObject head = new JSONObject(content.toString()); JSONObject ticker = head.getJSONObject("ticker"); Double avg = ticker.getDouble("avg"); // This is bitcoins priced in euros. We want FTC! avg *= btcRate; String s_avg = String.format("%.4f", avg).replace(',', '.'); rates.put("EUR", new ExchangeRate("EUR", Utils.toNanoCoins(s_avg), URL.getHost())); } finally { if (reader != null) reader.close(); } return rates; } catch (final IOException x) { x.printStackTrace(); } catch (final JSONException x) { x.printStackTrace(); } return null; }
From source file:eu.uqasar.service.dataadapter.CubesDataService.java
/** * Check if the connection to the provided URL is possible within the provided timeout * /*from ww w.ja v a 2 s . com*/ * @param url * @param timeout * @return True or False according to the connection success * */ private static boolean testConnectionToServer(String url, int timeout) { try { URL myUrl = new URL(url); URLConnection connection = myUrl.openConnection(); connection.setConnectTimeout(timeout); connection.connect(); return true; } catch (Exception e) { return false; } }