使用Java获取ping结果 [英] Getting the ping results using java

查看:900
本文介绍了使用Java获取ping结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取ping结果的最后一行. 我不是Java开发人员.我正在学习,所以请承担我的错误.

I'm trying to get the last line of the ping result. I'm not a java developer. I'm learning so please bear my mistakes.

这是我编写的程序.

private static String pingTest(String ip) {
    // TODO Auto-generated method stub
    String pingResult = "";
//  System.out.println("Came in pingTest");
    String pingCmd = "ping -c 3 " + ip;
    try {
        Runtime r = Runtime.getRuntime();
        Process p = r.exec(pingCmd);
        BufferedReader in = new BufferedReader(new
        InputStreamReader(p.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);                             
            pingResult += inputLine;
            }
        in.close();
    } catch (IOException e) {
        System.out.println(e);
    }
    return pingResult;

我得到的结果如下:

PING google.com(74.125.236.165)56(84)个字节的数据.64个字节 maa03s16-in-f5.1e100.net(74.125.236.165):icmp_req = 1 ttl = 52时间= 20.5 来自maa03s16-in-f5.1e100.net(74.125.236.165)的ms64字节:icmp_req = 2 来自maa03s16-in-f5.1e100.net的ttl = 52时间= 11.4 ms64字节 (74.125.236.165):icmp_req = 3 ttl = 52时间= 19.6毫秒--- google.com ping 统计信息--- 3个数据包传输,3个接收,0%数据包丢失,时间 2002msrtt min/avg/max/mdev = 11.494/17.252/20.579/4.089 m

PING google.com (74.125.236.165) 56(84) bytes of data.64 bytes from maa03s16-in-f5.1e100.net (74.125.236.165): icmp_req=1 ttl=52 time=20.5 ms64 bytes from maa03s16-in-f5.1e100.net (74.125.236.165): icmp_req=2 ttl=52 time=11.4 ms64 bytes from maa03s16-in-f5.1e100.net (74.125.236.165): icmp_req=3 ttl=52 time=19.6 ms--- google.com ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2002msrtt min/avg/max/mdev = 11.494/17.252/20.579/4.089 m

其格式正确.如何添加换行符? 出色地.我真正想要的是这样的东西.

Its ot properly formatted. how to add line breaks? Well. what I really want is something like this.

仅最后一行.

rtt min/avg/max/mdev = 20.774/20.962/21.085/0.135 ms

,我想在我的jsp页面中显示min,avg,max值.请引导我.

and I want to show the values of min,avg,max values in my jsp page. Please guide me.

推荐答案

尝试

pingResult += inputLine + "\n";

请注意,无论对性能有何影响,在这种情况下通常都使用StringBuilder

note that it's typical to use StringBuilder in such situations, no matter what perfomance impact is

这篇关于使用Java获取ping结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆