Java Utililty Methods URL Connection

List of utility methods to do URL Connection

Description

The list of methods to do URL Connection are organized into topic(s).

Method

InputStreamgetImageFromURL(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;
JarFilegetJar(String jarUrl)
get Jar
URL url = new URL("jar:" + jarUrl + "!/");
JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
JarFile jar = jarConnection.getJarFile();
return jar;
SetgetJarUrlForPackage(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());
...
longgetLastModified(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);
longgetLastModified(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 {
...
intgetLength(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;
...
OutputStreamgetOutputStream(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;
OutputStreamgetOutputStream(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);
...
StringgetPage(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;
...