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)
======================================================================
- duplicates
-
JDK-4160499 sun.net.www.protocol.http.HttpURLConnection error handling
- Resolved