JDK-4191207 : URLConnection.getContent() doesn't throw FileNotFoundException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.1.4
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.6
  • CPU: sparc
  • Submitted: 1998-11-19
  • Updated: 2001-01-30
  • Resolved: 2001-01-30
Related Reports
Duplicate :  
Description

Name: jn10789			Date: 11/18/98


It should throw an exception if it cannot find
the file.  Note that it does throw an exception if
the file extension is .html, but for other file
types, such as .gif or .txt, it doesn't throw the
exception.

Here is the code below:


import java.net.*;
import java.io.IOException;

public class Verify {

         private void verifyURL(URL path) throws Exception
         {	   
		   URLConnection uc = (URLConnection) path.openConnection();
		   if (uc instanceof HttpURLConnection) 
		   {
			int responseCode = ((HttpURLConnection)uc).getResponseCode();
			if (responseCode != HttpURLConnection.HTTP_OK) 
			{
				System.out.println("thowing exception");
				throw new Exception("Cannot obtain file, " + path + ", from the network.  \n" +
				      "HTTP Error " + responseCode + "  " + ((HttpURLConnection)uc).getResponseMessage());
			}
		   }
		   else
		   {
			uc.getContent();	  // Verify the file exists.  Throws an exception if it doesn't.
		   }

	 }   // *** End of method verifyURL(URL path) ***

  public static void main( String args[] ) {
    Verify v = new Verify();
    try 
      {
	v.verifyURL(new URL("file:/export/home/htdocs/LOTSwkpl/com/lotus/desktop/main/images/splash.gif"));
      }
    catch (MalformedURLException e) { System.out.println("Malform: " + e); }
    catch (IOException eIO) { System.out.println("IO: " + eIO); }
    catch (Exception ex) { System.out.println("Ex: " + ex); }
    
  }

}
(Review ID: 40050)
======================================================================

Comments
EVALUATION This bug has already been fixed in merlin as part of 4160499. The new behaviour is that a FNF is throw for genuine "not found" http errors (404 and 410) and this is irrespective of the file type. For other http errors a more general IOException is thrown indicating the error code and the URL that caused the error. The application reads the error page returned by the server using the error stream (obtained using getErrorStream). In addition getResponseCode() has been fixed so that it doesn't throw an exception if the input stream hasn't been obtained. alan.bateman@ireland 2001-01-30
30-01-2001