List of usage examples for java.net URLConnection connect
public abstract void connect() throws IOException;
From source file:com.ettoremastrogiacomo.sktradingjava.starters.Temp.java
public static void testCookies() throws Exception { URL url = new URL("http://www.repubblica.it"); URLConnection conn = url.openConnection(); //conn.setRequestProperty("Cookie", "name1=value1; name2=value2"); conn.connect(); Map<String, List<String>> map = conn.getHeaderFields(); map.keySet().forEach((s) -> {//from w w w . ja va 2 s. c o m LOG.debug(s + "\t" + map.get(s)); }); }
From source file:com.baidu.api.client.core.DownloadUtil.java
/** * Download the file pointed by the url and return the content as byte array. * * @param strUrl The Url to be downloaded. * @param connectTimeout Connect timeout in milliseconds. * @param readTimeout Read timeout in milliseconds. * @param maxFileSize Max file size in BYTE. * @return The file content as byte array. *//*from w w w. jav a 2 s . c om*/ public static byte[] downloadFile(String strUrl, int connectTimeout, int readTimeout, int maxFileSize) { InputStream in = null; try { URL url = new URL(strUrl); // URL? URLConnection ucon = url.openConnection(); ucon.setConnectTimeout(connectTimeout); ucon.setReadTimeout(readTimeout); ucon.connect(); if (ucon.getContentLength() > maxFileSize) { String msg = "File " + strUrl + " size [" + ucon.getContentLength() + "] too large, download stoped."; throw new ClientInternalException(msg); } if (ucon instanceof HttpURLConnection) { HttpURLConnection httpCon = (HttpURLConnection) ucon; if (httpCon.getResponseCode() > 399) { String msg = "Failed to download file " + strUrl + " server response " + httpCon.getResponseMessage(); throw new ClientInternalException(msg); } } in = ucon.getInputStream(); // ? byte[] byteBuf = new byte[BUFFER_SIZE]; byte[] ret = null; int count, total = 0; // ?? while ((count = in.read(byteBuf, total, BUFFER_SIZE - total)) > 0) { total += count; if (total + 124 >= BUFFER_SIZE) break; } if (total < BUFFER_SIZE - 124) { ret = ArrayUtils.subarray(byteBuf, 0, total); } else { ByteArrayOutputStream bos = new ByteArrayOutputStream(MATERIAL_SIZE); count = total; total = 0; do { bos.write(byteBuf, 0, count); total += count; if (total > maxFileSize) { String msg = "File " + strUrl + " size exceed [" + maxFileSize + "] download stoped."; throw new ClientInternalException(msg); } } while ((count = in.read(byteBuf)) > 0); ret = bos.toByteArray(); } if (ret.length < MIN_SIZE) { String msg = "File " + strUrl + " size [" + maxFileSize + "] too small."; throw new ClientInternalException(msg); } return ret; } catch (IOException e) { String msg = "Failed to download file " + strUrl + " msg=" + e.getMessage(); throw new ClientInternalException(msg); } finally { try { if (in != null) in.close(); } catch (IOException e) { // ? System.out.println("Exception while close url - " + e.getMessage()); } } }
From source file:org.openlegacy.designtime.newproject.NewProjectMetadataRetriever.java
private static InputStream getUrlConnectionInputStream(String urlPath) throws IOException { URL url = new URL(urlPath); URLConnection con = url.openConnection(); con.setDoInput(true);//from w w w . j av a2 s. c o m con.setDoOutput(false); con.connect(); return con.getInputStream(); }
From source file:com.iwgame.iwcloud.baidu.task.util.DownloadUtil.java
/** * url/*from w w w . j a va 2 s . c o m*/ * @param strUrl * The Url to be downloaded. * @param connectTimeout * Connect timeout in milliseconds. * @param readTimeout * Read timeout in milliseconds. * @param maxFileSize * Max file size in BYTE. * @return The file content as byte array. */ public static byte[] downloadFile(String strUrl, int connectTimeout, int readTimeout, int maxFileSize) throws IOException { InputStream in = null; try { URL url = new URL(strUrl); // URL? URLConnection ucon = url.openConnection(); ucon.setConnectTimeout(connectTimeout); ucon.setReadTimeout(readTimeout); ucon.connect(); if (ucon.getContentLength() > maxFileSize) { String msg = "File " + strUrl + " size [" + ucon.getContentLength() + "] too large, download stoped."; logger.error(msg); throw new ClientInternalException(msg); } if (ucon instanceof HttpURLConnection) { HttpURLConnection httpCon = (HttpURLConnection) ucon; if (httpCon.getResponseCode() > 399) { String msg = "Failed to download file " + strUrl + " server response " + httpCon.getResponseMessage(); logger.error(msg); throw new ClientInternalException(msg); } } in = ucon.getInputStream(); // ? byte[] byteBuf = new byte[BUFFER_SIZE]; byte[] ret = null; int count, total = 0; // ?? while ((count = in.read(byteBuf, total, BUFFER_SIZE - total)) > 0) { total += count; if (total + 124 >= BUFFER_SIZE) break; } if (total < BUFFER_SIZE - 124) { ret = ArrayUtils.subarray(byteBuf, 0, total); } else { ByteArrayOutputStream bos = new ByteArrayOutputStream(MATERIAL_SIZE); count = total; total = 0; do { bos.write(byteBuf, 0, count); total += count; if (total > maxFileSize) { String msg = "File " + strUrl + " size exceed [" + maxFileSize + "] download stoped."; logger.error(msg); throw new ClientInternalException(msg); } } while ((count = in.read(byteBuf)) > 0); ret = bos.toByteArray(); } if (ret.length < MIN_SIZE) { String msg = "File " + strUrl + " size [" + maxFileSize + "] too small."; logger.error(msg); throw new ClientInternalException(msg); } return ret; } catch (IOException e) { String msg = "Failed to download file " + strUrl + " msg=" + e.getMessage(); logger.error(msg); throw e; } finally { try { if (in != null) { in.close(); } } catch (IOException e) { logger.error("Exception while close url - ", e); throw e; } } }
From source file:ro.agrade.jira.qanda.utils.ResourceUtils.java
/** * Gets the resource/* w ww. jav a 2s. co m*/ * * @param resFileName file path, URL, or internal resource denominator. * @param loaderClazz the loader class * @throws IOException if something goes wrong. * @return the configuration as an input stream */ public static InputStream getAsInputStream(String resFileName, Class<?> loaderClazz) throws IOException { //first, try to see if this is a file File f = new File(resFileName); if (f.exists()) { return new BufferedInputStream(new FileInputStream(f)); } //fallback to URL config URL configurationUrl = null; if (resFileName.startsWith("/")) { configurationUrl = loaderClazz.getResource(resFileName); if (configurationUrl == null) { String msg = String.format("The resource >>%s<< is not a valid resource.", resFileName); LOG.error(msg); throw new java.io.IOException(msg); } } else { try { configurationUrl = new URL(resFileName); } catch (MalformedURLException ex) { String msg = String.format("The resource >>%s<< is not a valid URL.", resFileName); LOG.error(msg, ex); throw new IOException(msg); } } //we have the URL, connect on it ... URLConnection conn = configurationUrl.openConnection(); conn.connect(); return conn.getInputStream(); }
From source file:imageLines.ImageHelpers.java
public static String getCookie(String urlString) throws MalformedURLException, IOException { URL url = new URL(urlString); URLConnection conn = url.openConnection(); conn.connect(); Map<String, List<String>> headers = conn.getHeaderFields(); List<String> values = headers.get("Set-Cookie"); String cookieValue = null;/*from w w w . j ava2 s. c o m*/ for (Iterator iter = values.iterator(); iter.hasNext();) { String v = iter.next().toString(); return v; } return ""; }
From source file:org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil.java
private static Object buildRepository(String repositoryFileName, Class targetRepository) throws IOException, ParserConfigurationException, SAXException { URL url = buildURL(repositoryFileName); String pathName = url.toExternalForm(); LOG.debug("Building repository from :" + pathName); InputSource source = new InputSource(pathName); URLConnection conn = url.openConnection(); conn.setUseCaches(false);/*from w w w.j av a 2s . c o m*/ conn.connect(); InputStream i = conn.getInputStream(); source.setByteStream(i); try { return readMetadataFromXML(source, targetRepository); } finally { try { i.close(); } catch (IOException x) { LOG.warn("unable to close repository input stream [" + x.getMessage() + "]", x); } } }
From source file:org.gridgain.testsuites.bamboo.GridHadoopTestSuite.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./*w ww. j a v a2s . c o m*/ */ private static void download(String appName, String homeVariable, String downloadPath, String destName) throws Exception { String homeVal = GridSystemProperties.getString(homeVariable); if (!F.isEmpty(homeVal) && new File(homeVal).isDirectory()) { X.println(homeVariable + " is set to: " + homeVal); return; } List<String> urls = F.asList("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); File install = new File(tmpPath + File.separatorChar + "__hadoop"); File home = new File(install, destName); X.println("Setting " + homeVariable + " to " + home.getAbsolutePath()); System.setProperty(homeVariable, home.getAbsolutePath()); 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: " + install.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 { 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(install); } } throw new IllegalStateException("Failed to install " + appName + "."); }
From source file:net.grinder.util.NetworkUtil.java
/** * Download a file from the given URL string. * //from w ww . j a v a 2 s . c om * @param urlString * URL string * @param toFile * file to be saved. */ public static void downloadFile(String urlString, File toFile) { FileOutputStream os = null; InputStream in = null; URLConnection connection = null; try { URL url = new URL(urlString); connection = url.openConnection(); connection.connect(); byte[] buffer = new byte[4 * 1024]; int read; os = new FileOutputStream(toFile); in = connection.getInputStream(); while ((read = in.read(buffer)) > 0) { os.write(buffer, 0, read); } } catch (Exception e) { LOGGER.error("download file from {} was failed", urlString, e); throw new NGrinderRuntimeException("Error while download " + urlString, e); } finally { ((HttpURLConnection) connection).disconnect(); IOUtils.closeQuietly(os); IOUtils.closeQuietly(in); } return; }
From source file:org.connectbot.util.UpdateHelper.java
/** * Read contents of a URL and return as a String. Handles any server * downtime with a 6-second timeout.// w w w . jav a 2 s. co m */ private static String getUrl(String tryUrl, String userAgent) throws Exception { URL url = new URL(tryUrl); URLConnection connection = url.openConnection(); connection.setConnectTimeout(6000); connection.setReadTimeout(6000); connection.setRequestProperty("User-Agent", userAgent); connection.connect(); InputStream is = connection.getInputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream(); int bytesRead; byte[] buffer = new byte[1024]; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } os.flush(); os.close(); is.close(); return new String(os.toByteArray()); }