String command="ping 127.0.0.1";
Process process = Runtime.getRuntime().exec(command);
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = bufferedreader.readLine();
for(;line != null;) {
String nextLine = bufferedreader.readLine();
//在此处理获得的ping反馈信息,比如下边的打印
System.out.println(line);
line = nextLine;
}
bufferedreader.close();简单的例子,不过我想足够了吧?

解决方案 »

  1.   

    我只要一次ping的时间,怎么改?
      

  2.   

    请自己了解一下ping命令的用法,并自己了解一下java.lang.String类的各种方法。要知道,你的上司不给我发薪水
      

  3.   

    从查询得到结果的问题,你是指数据库搜索吗?
    如果是的话,可以在ResultSet.executeQuery();调用前后截取系统时间并进行相关处理就是了阿。
    不过我没做过相关的,意见仅供参考
      

  4.   

    to tingod(┞┧○┟┦○) :
    第二个问题解决就是你所说的那样啊!第一个问题解决解决如下:
    <%@ page import="java.io.*" %><%
      Runtime runtime = Runtime.getRuntime();
      Process process =null;
      String line=null;
      InputStream is =null;
      InputStreamReader isr=null;
      BufferedReader br =null;
      String ip="你要ping的ip";
      //自行修改上面的ip位址
      try
      {
        process =runtime.exec("ping "+ip);
        is = process.getInputStream();
        isr=new InputStreamReader(is);
        br =new BufferedReader(isr);
        out.println("<pre>");
        while( (line = br.readLine()) != null )
        {
          out.println(line);
          out.flush();
        }
        out.println("</pre>");
        is.close();
        isr.close();
        br.close();
      }
      catch(IOException e )
      {
        out.println(e);
        runtime.exit(1);
      }
    %>
      

  5.   

    不过得到的是:
    Reply from 202.112.0.36: bytes=32 time=47ms TTL=246Reply from 202.112.0.36: bytes=32 time=78ms TTL=246Reply from 202.112.0.36: bytes=32 time=141ms TTL=246Reply from 202.112.0.36: bytes=32 time=47ms TTL=246Ping statistics for 202.112.0.36:    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),Approximate round trip times in milli-seconds:    Minimum = 47ms, Maximum =  141ms, Average =  78ms
      

  6.   

    你可以根据ping命令的返回信息的方式来获取一行
    Reply from 202.112.0.36: bytes=32 time=78ms TTL=246
    信息,然后可以用String的各种方法来获得'time=78ms'中的78ms这个信息,如果这就是你想要的所谓“一次ping时间”的话
      

  7.   

    这样可以
        while( (line = br.readLine()) != null )
        {
          if(line.lastIndexOf("Reply")!=-1){
          String time;
          time=line.substring(line.lastIndexOf("time")+5,line.lastIndexOf("TTL")/*若要仅得到数值,此处加上 -3 */);
          out.println(time);
          out.flush();
          return;
        }
      

  8.   

    有错哦
    C:\bea\user_projects\mydomain\myserver\.wlnotdelete\DefaultWebApp_DefaultWebApp_5982438\jsp_servlet\_main\__u_search.java:128: 'catch' without 'try'
    probably occurred due to an error in /main/U_search.jsp line 38:
    catch(IOException e )C:\bea\user_projects\mydomain\myserver\.wlnotdelete\DefaultWebApp_DefaultWebApp_5982438\jsp_servlet\_main\__u_search.java:88: 'try' without 'catch' or 'finally'
    (No more information available, probably caused by another error)C:\bea\user_projects\mydomain\myserver\.wlnotdelete\DefaultWebApp_DefaultWebApp_5982438\jsp_servlet\_main\__u_search.java:157: '}' expected
    (No more information available, probably caused by another error)
      

  9.   

    在上面while块的最后边加一个
    }