Java URLConnection Create getURLConnection(URL url)

Here you can find the source of getURLConnection(URL url)

Description

Returns a URL connection that an input stream can be obtained from.

License

Open Source License

Parameter

Parameter Description
url URL to open connection to

Exception

Parameter Description
MalformedURLException if the url is null
IOException if there is a problem accessing the resource specified by the url

Return

the url connection

Declaration

public static URLConnection getURLConnection(URL url) throws MalformedURLException, IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  Copyright (c) 2006, 2010 IBM Corporation and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 * /*from   ww w. ja  va2s  .  co m*/
 *  Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.IOException;

import java.net.*;

public class Main {
    /**
     * Returns a URL connection that an input stream can be obtained from.  The
     * URL Connection can handle urls of a variety of types including files, jar
     * files and remote urls.
     * <p>
     * NOTE: If the connection is of type {@link JarURLConnection} the zip file
     * should be independantly closed using {@link JarURLConnection#getJarFile()}.close()
     * https://bugs.eclipse.org/bugs/show_bug.cgi?id=326263
     * </p>
     * 
     * @param url URL to open connection to
     * @return the url connection
     * @throws MalformedURLException if the url is null
     * @throws IOException if there is a problem accessing the resource specified by the url
     */
    public static URLConnection getURLConnection(URL url) throws MalformedURLException, IOException {
        if (url == null) {
            throw new MalformedURLException("URL specified is null"); //$NON-NLS-1$
        }
        URLConnection connection = url.openConnection();
        if (connection instanceof JarURLConnection) {
            connection.setUseCaches(false);
        }
        return connection;
    }
}

Related

  1. createConnection(URL url)
  2. createConnection(URL url)
  3. createConnection(URL url, Proxy proxy)
  4. getURLConnection(String uri)
  5. getUrlConnection(String urlString)
  6. openConnection(final URL url)
  7. openConnection(String urlPath)
  8. openConnection(URL localURL)
  9. openConnection(URL url)