List of utility methods to do URL Connection
InputStream | getImageFromURL(String remoteURL) get Image From URL InputStream inputStream = null; try { URL url = new URL(remoteURL); URLConnection conn = url.openConnection(); inputStream = conn.getInputStream(); } catch (Exception e) { e.printStackTrace(); return inputStream; |
JarFile | getJar(String jarUrl) get Jar URL url = new URL("jar:" + jarUrl + "!/"); JarURLConnection jarConnection = (JarURLConnection) url.openConnection(); JarFile jar = jarConnection.getJarFile(); return jar; |
Set | getJarUrlForPackage(String packageName) get Jar Url For Package URLClassLoader loader = (URLClassLoader) Thread.currentThread().getContextClassLoader(); Set<URL> result = new HashSet<URL>(); try { Enumeration<URL> urls = loader.getResources(packageName); while (urls.hasMoreElements()) { URL url = urls.nextElement(); JarURLConnection connection = (JarURLConnection) url.openConnection(); result.add(connection.getJarFileURL()); ... |
long | getLastModified(URL url) Returns the last modified time stamp for the passed URL URLConnection conn = null; try { conn = nvl(url, "Passed URL was null").openConnection(); return conn.getLastModified(); } catch (Exception e) { throw new RuntimeException("Failed to get LastModified for [" + url + "]", e); |
long | getLastModified(URLConnection connection) get Last Modified long modified; if (connection instanceof JarURLConnection) { URL jarFileUrl = ((JarURLConnection) connection).getJarFileURL(); URLConnection jarFileConnection = jarFileUrl.openConnection(); try { modified = jarFileConnection.getLastModified(); } finally { try { ... |
int | getLength(String htmlUrl) get Length URL url = new URL(htmlUrl); URLConnection con = url.openConnection(); return con.getContentLength(); |
String[] | getLines(final URL url, final int linesToRead) Reads the first n lines from the resource pointed to by the passed URL
if (url == null) throw new IllegalArgumentException("The passed URL was null"); if (linesToRead < 1) throw new IllegalArgumentException("Invalid number of lines [" + linesToRead + "]. Must be >= 1"); InputStream is = null; BufferedReader br = null; InputStreamReader isr = null; URLConnection connection = null; ... |
OutputStream | getOutputStream(final URL outputURL) get Output Stream OutputStream outputStream; final URLConnection connection = outputURL.openConnection(); connection.setDoOutput(true); try { outputStream = connection.getOutputStream(); } catch (final UnknownServiceException e) { outputStream = new FileOutputStream(outputURL.getPath()); return outputStream; |
OutputStream | getOutputStream(URL url) get Output Stream if (isFile(url)) { File file = new File(url.getPath()); if (!file.exists()) { File parent = file.getParentFile(); if (parent != null && !parent.exists()) parent.mkdirs(); return new FileOutputStream(file); ... |
String | getPage(String url) get Page String result = ""; try { URL url1 = new URL(url); URLConnection urlConn = url1.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); String text; while ((text = in.readLine()) != null) { result = result + text; ... |