URL url; InputStream iss; int i; long lSize; byte[] byteArr; String ss; try { url = new URL("http://htgzln059.htgz.com/qms/test/getTime.jsp"); iss = url.openStream(); byteArr = new byte[5000]; iss.read(byteArr,0,5000); ss = new String(byteArr); if(ss.indexOf("<TITLE>404 Not Found</TITLE>")>=0) System.out.println ("not found");
InputStream iss;
int i;
long lSize;
byte[] byteArr;
String ss;
try
{
url = new URL("http://htgzln059.htgz.com/qms/test/getTime.jsp");
iss = url.openStream();
byteArr = new byte[5000]; iss.read(byteArr,0,5000);
ss = new String(byteArr);
if(ss.indexOf("<TITLE>404 Not Found</TITLE>")>=0)
System.out.println ("not found");
}catch(Exception e)
{
System.out.println (e.toString());
}
我把URL转换成物理路径,在程序中判断那个路径的文件存不存在是不是也可以?
呵呵
找不到会:
java.io.FileNotFoundException
也可以抛出异常吗?
处理方法和上面例子同理.
[JDK1.1] import java.net.*;
import java.io.*;
import java.net.*;public class Check {public static void main(String s[]) {
System.out.println(exists("http://www.csdn.net/index.html"));
System.out.println(exists("http://www.csdn.com/pagenotfound.html"));
}static boolean exists(String URLName){
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con =
(HttpURLConnection) new URL(URLName).openConnection();
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
}