新手提问,java中判断网络连接是否正常 急 如题,在java中,如何判定网络是否正常,比如,指定一个ip地址,判断是否能连接上。大家最好给出源码,因为是个新手,如果是思路,还要再找资料,等我先完成任务再消化内容 谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;import java.net.*;public class Test{ public static void main(String[] args) { URL url = null; try { url = new URL("http://google.com/"); InputStream in = url.openStream(); System.out.println("连接正常"); in.close(); } catch (IOException e) { System.out.println("无法连接到:" + url.toString()); } }} 谢谢CrazyGou() 的恢复.你的恢复是不是只针对 url啊,如果就是一个普通的ip地址呢?换成一个连通的地址,他仍然提示连接不上 public class Test{ public static void main(String[] args) { String ip = "192.168.1.1"; //ip = "google.com"; try { Process p = Runtime.getRuntime().exec("cmd /c ping -n 1 " + ip); //此处1变大可以增加精确度,但影响测试速度 BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); String temp = null; StringBuffer strBuffer = new StringBuffer(); while ((temp = (in.readLine())) != null) strBuffer.append(temp); //System.out.println(strBuffer); if (strBuffer.toString().matches(".*\\(\\d?\\d% loss\\).*")) { System.out.println("正常"); } else { System.out.println("不正常"); } } catch (IOException e) { e.printStackTrace(); } }} import java.net.*;import java.io.*;public class IPTest{ public static void main(String[] args)throws IOException { try { InetAddress ad = InetAddress.getByName("192.168.1.119"); boolean state = ad.isReachable(5000);//测试是否可以达到该地址 if(state) System.out.println("连接成功" + ad.getHostAddress()); else System.err.println("连接失败"); } catch(UnknownHostException e) { System.err.println("连接失败"); } }}实现尽最大努力试图到达主机,但防火墙和服务器配置可能阻塞请求,使其在某些特定的端口可以访问时处于不可到达状态。 eclipse+tomcat写servlet程序helloworld问题 java Thread 里面的变量能不能访问? Socket接收网页表单提交中文无法正常显示 关于数据库完整性 如何用j2ee部署网站? 提问?并发处理相关... 急!!socket网络编程问题,在线! Java中放图片的问题? 要activate key! 在JAVA里,一直没有找到实现文件上传的功能????? 一个很急的问题,请高手看看 多个TextField是用FocusAdapter进行验证,急急急.........................
import java.net.*;public class Test
{
public static void main(String[] args)
{
URL url = null;
try {
url = new URL("http://google.com/");
InputStream in = url.openStream();
System.out.println("连接正常");
in.close();
} catch (IOException e) {
System.out.println("无法连接到:" + url.toString());
}
}
}
换成一个连通的地址,他仍然提示连接不上
{
public static void main(String[] args)
{
String ip = "192.168.1.1";
//ip = "google.com";
try {
Process p = Runtime.getRuntime().exec("cmd /c ping -n 1 " + ip); //此处1变大可以增加精确度,但影响测试速度
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String temp = null;
StringBuffer strBuffer = new StringBuffer();
while ((temp = (in.readLine())) != null)
strBuffer.append(temp);
//System.out.println(strBuffer);
if (strBuffer.toString().matches(".*\\(\\d?\\d% loss\\).*")) {
System.out.println("正常");
}
else {
System.out.println("不正常");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
public class IPTest
{
public static void main(String[] args)throws IOException
{
try
{
InetAddress ad = InetAddress.getByName("192.168.1.119");
boolean state = ad.isReachable(5000);//测试是否可以达到该地址
if(state)
System.out.println("连接成功" + ad.getHostAddress());
else
System.err.println("连接失败");
}
catch(UnknownHostException e)
{
System.err.println("连接失败");
}
}
}实现尽最大努力试图到达主机,但防火墙和服务器配置可能阻塞请求,使其在某些特定的端口可以访问时处于不可到达状态。