Here you can find the source of getInputStream(URL urlFile)
Parameter | Description |
---|---|
urlFile | the URL to get the input stream from. |
Parameter | Description |
---|---|
IOException | if attempt to open the file denoted by URL has failed. |
ConnectException | if trouble connecting to URL. |
public static InputStream getInputStream(URL urlFile) throws IOException, ConnectException
//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; } }