原代码可以。稍微改了一点点,观察过程import java.net.*;
import java.io.*;public class TestURL {
public static void main(String args[]) {
String aURL = "http://www.sinawww.com/load.txt";
TestURL tu = new TestURL();
if (args.length > 0 && args[0].length() > 10) aURL = args[0];
System.out.println("Does the file: " + aURL + " exist? " + tu.DoesURLExist(aURL));
}
public String DoesURLExist(String aURL) {
String a = "";
try {
//A URL object is created containing the URL of a Web page
URL url = new URL(aURL);
//A connection is made to the website
URLConnection con = url.openConnection();
// Try to open the file
InputStream in = url.openStream();
in.read();
in.close();
a = "YES";
} catch (ConnectException ce) {
//System.out.println("ce: " + ce);
a = "Connection refused";
return a;
} catch (UnknownHostException uhe) {
//System.out.println("uhe: " + uhe);
a = "Host unknown";
return a;
} catch (IOException ioe) {
ioe.printStackTrace();
if (a.length() == 0 ) a = "NO";
}
return a;
}
}运行结果:F:\java>javac TestURL.javaF:\java>java TestURL http://expert.csdn.net/Expert/topic/1528/99999999.xml?temp=
.0
java.io.FileNotFoundException: http://expert.csdn.net/Expert/topic/1528/99999999
.xml?temp=.0
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
nection.java:707)
at java.net.URL.openStream(URL.java:960)
at TestURL.DoesURLExist(TestURL.java:19)
at TestURL.main(TestURL.java:9)
Does the file: http://expert.csdn.net/Expert/topic/1528/99999999.xml?temp=.0 exi
st? NOF:\java>java TestURL http://expert.csdn.net/Expert/topic/1528/1528111.xml?temp=.
7368891
Does the file: http://expert.csdn.net/Expert/topic/1528/1528111.xml?temp=.736889
1 exist? YES
import java.io.*;public class TestURL {
public static void main(String args[]) {
String aURL = "http://www.sinawww.com/load.txt";
TestURL tu = new TestURL();
if (args.length > 0 && args[0].length() > 10) aURL = args[0];
System.out.println("Does the file: " + aURL + " exist? " + tu.DoesURLExist(aURL));
}
public String DoesURLExist(String aURL) {
String a = "";
try {
//A URL object is created containing the URL of a Web page
URL url = new URL(aURL);
//A connection is made to the website
URLConnection con = url.openConnection();
// Try to open the file
InputStream in = url.openStream();
in.read();
in.close();
a = "YES";
} catch (ConnectException ce) {
//System.out.println("ce: " + ce);
a = "Connection refused";
return a;
} catch (UnknownHostException uhe) {
//System.out.println("uhe: " + uhe);
a = "Host unknown";
return a;
} catch (IOException ioe) {
ioe.printStackTrace();
if (a.length() == 0 ) a = "NO";
}
return a;
}
}运行结果:F:\java>javac TestURL.javaF:\java>java TestURL http://expert.csdn.net/Expert/topic/1528/99999999.xml?temp=
.0
java.io.FileNotFoundException: http://expert.csdn.net/Expert/topic/1528/99999999
.xml?temp=.0
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
nection.java:707)
at java.net.URL.openStream(URL.java:960)
at TestURL.DoesURLExist(TestURL.java:19)
at TestURL.main(TestURL.java:9)
Does the file: http://expert.csdn.net/Expert/topic/1528/99999999.xml?temp=.0 exi
st? NOF:\java>java TestURL http://expert.csdn.net/Expert/topic/1528/1528111.xml?temp=.
7368891
Does the file: http://expert.csdn.net/Expert/topic/1528/1528111.xml?temp=.736889
1 exist? YES
得到的错误信息是:
F:\java>ping www.sinawww.com
Unknown host www.sinawww.com.