Java URL to InputStream getInputStream(URL urlFile)

Here you can find the source of getInputStream(URL urlFile)

Description

Get the input stream from a URL.

License

Open Source License

Parameter

Parameter Description
urlFile the URL to get the input stream from.

Exception

Parameter Description
IOException if attempt to open the file denoted by URL has failed.
ConnectException if trouble connecting to URL.

Return

the input stream corresponding to the given URL.

Declaration

public static InputStream getInputStream(URL urlFile) throws IOException, ConnectException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *
 * Copyright (c) 2002, 2008 IBM Corporation, Beacon Information Technology Inc. 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
 *
 * Contributors:/*from w ww. j a  v a 2 s .  c  om*/
 *   IBM      - Initial API and implementation
 *   BeaconIT - Initial API and implementation
 *******************************************************************************/

import java.io.BufferedInputStream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import java.net.ConnectException;

import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    /**
    * Get contents of a resource and return as a input stream.
    *
    * @param resourceName the name of the resource to get and return as
    *                      an input stream.
    * @return contents of a resource as an input stream.
    * @throws IOException if the resource could not be located.
    */
    public static InputStream getInputStream(String resourceName) throws IOException {
        InputStream is = null;

        // If resource reference is a URL, then input stream from URL
        try {
            // Try to create URL
            URL urlResource = new URL(resourceName);

            // If successful, then get URL input stream
            is = getInputStream(urlResource);
        }

        // Else try to read resource directly
        catch (MalformedURLException mue) {
            boolean bTryClassLoader = false;

            try {
                // Open file input stream
                is = new BufferedInputStream(new FileInputStream(resourceName));
            } catch (FileNotFoundException fnfe) {
                // Set try class loader flag
                bTryClassLoader = true;
            } catch (SecurityException se) {
                // Set try class loader flag
                bTryClassLoader = true;
            } catch (Exception e) {
                // DEBUG:
                System.out.println("Exception in getInputStream :" + e.toString());
            }

            // If try class loader, then use it to get input stream
            if (bTryClassLoader) {
                // Use class loader to load resource
                is = ClassLoader.getSystemResourceAsStream(resourceName);
            }
        }

        // If the input stream is null, then throw FileNotFoundException
        if (is == null) {
            //try this
            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
        }

        // If the input stream is null, then throw FileNotFoundException
        if (is == null) {
            //try this
            URL aURL = Thread.currentThread().getContextClassLoader().getResource(resourceName);
            if (aURL != null)
                is = getInputStream(aURL);
        }

        if (is == null)
            // Throw execption
            throw new FileNotFoundException("Could not locate resource file: " + resourceName);

        // Return input stream
        return is;
    }

    /**
     * Get the input stream from a URL.
     * @param urlFile the URL to get the input stream from.
     * @return the input stream corresponding to the given URL.
     * @throws IOException if attempt to open the file denoted by URL has failed.
     * @throws ConnectException if trouble connecting to URL.
     */
    public static InputStream getInputStream(URL urlFile) throws IOException, ConnectException {
        InputStream is = null;

        // ADD: how are URLs that are password protected handled????

        try {
            // Open file input stream
            is = new BufferedInputStream(urlFile.openStream());
        }

        catch (ConnectException e) {
            // Re-throw this excpetion with additional information
            throw new java.net.ConnectException("Could not connect to URL: " + urlFile.toExternalForm() + ".");
        }

        // Return input stream
        return is;
    }
}

Related

  1. getInputStream(URL sourceURL)
  2. getInputStream(URL url)
  3. getInputStream(URL url)
  4. getInputStream(URL url)
  5. getInputStream(URL url)
  6. getInputStream(URLConnection c)
  7. getInputStream(URLConnection connection)
  8. getInputStream(URLConnection paramURLConnection)
  9. getInputStreamForURL(URL url)