connect to the server,
HEAD the document, if no error message, the document exists

解决方案 »

  1.   

    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");

    }catch(Exception e)
    {
    System.out.println (e.toString());
    }
      

  2.   

    上面"<TITLE>404 Not Found</TITLE>"根据SERVER的出错信息而定.
      

  3.   

    谢谢!
    我把URL转换成物理路径,在程序中判断那个路径的文件存不存在是不是也可以?
    呵呵
      

  4.   

    url = new URL("file:///c://autoexec.bat");
    找不到会:
    java.io.FileNotFoundException
      

  5.   

    那url=new URL("http://localhost:8080/***/xyz.doc")
    也可以抛出异常吗?
      

  6.   

    不会.因为你这个是HTTP协议,刚才我说那个是FILE协议.url=new URL("http://localhost:8080/***/xyz.doc")
    处理方法和上面例子同理.
      

  7.   

    Check if a page exists
    [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;
           }
      }
     }