Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4191207

URLConnection.getContent() doesn't throw FileNotFoundException

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.4
    • core-libs
    • sparc
    • solaris_2.6



      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)
      ======================================================================

            alanb Alan Bateman
            jdn Jeffrey Nisewanger (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: