64位系统中java调用ping命令,Java调用ping命令

package cn.com.songjy;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Ping {

public static void main(String[] args) throws IOException {

System.out.println(ping("127.0.0.1", 1));

System.out.println("************************************");

System.out.println(ping("www.baidu.com", 35));

}

/**

*

* @param target_name

* IP地址或域名

* @param out_time

* 超时间隔,单位为毫秒

* @return

* @throws IOException

*/

public static boolean ping(String target_name, int out_time)

throws IOException {

Runtime runtime = Runtime.getRuntime();

String ping_command = "ping " + target_name + " -w " + out_time;

System.out.println("命令格式:" + ping_command);

Process process = runtime.exec(ping_command);

if (null == process)

return false;

BufferedReader bufferedReader = new BufferedReader(

new InputStreamReader(process.getInputStream(), "GBK"));// windows下编码默认是GBK,Linux是UTF-8

String line = null;

while (null != (line = bufferedReader.readLine())) {

System.out.println(line);

if (line.startsWith("Reply from"))

return true;

if (line.startsWith("来自"))

return true;

}

bufferedReader.close();

return false;

}

}

/* 这里只是简单检测是否能通,如果还需要更详细信息可查看ping命令的介绍 */

效果图如下:

0818b9ca8b590ca3270a3433284dd417.png

以下来自

java Ping域名网络工具类PingUtils.java源代码分享

package demo;

import java.net.InetAddress;

import java.net.UnknownHostException;

public class PingUtils {

public static Domain ping(String domian) {

long start = System.currentTimeMillis();

Domain result = new Domain();

try {

InetAddress address = InetAddress.getByName(domian);

result.ip = address.getHostAddress();

result.host = address.getHostName();

long end = System.currentTimeMillis();

result.time = (end - start);

} catch (UnknownHostException e) {

result.ip = "0.0.0.0";

result.host = "UNKONW";

}

return result;

}

public static class Domain {

String ip;

String host;

long time;

@Override

public String toString() {

return String.format("host=%s, ip=%s, time=%s", host, ip, time);

}

}

public static void main(String[] args) {

Domain domain = ping("www.baidu.com");

System.out.println(domain);

}

}

效果如下:

0818b9ca8b590ca3270a3433284dd417.png

java 调用shell命令

java使用dos命令创建文件夹

JAVA执行bat文件和shell脚本文件

del命令和rd命令

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值