模拟Ping
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class IpContimeUtil {

    private static float ping(String ipAddress) {

        boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");
        String command = String.format("ping -%s 1 %s", isWindows ? "n" : "c", ipAddress);
        try {
            Process process = Runtime.getRuntime().exec(command);
            InputStream inputStream = process.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "GBK");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            Pattern pattern = Pattern.compile(".*?([\\d.]+)\\s?ms.*");

            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
                Matcher matcher = pattern.matcher(line);
                if (matcher.matches()) {
                    return Float.parseFloat(matcher.group(1));
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("响应超时");
        }
        throw new RuntimeException("响应异常");
    }

    private static List<Float> print(List<String> newList) throws Exception {
        return newList.stream().map(IpContimeUtil::ping).collect(Collectors.toList());
    }

    public static void main(String[] args) throws Exception {
        System.out.println("响应时间:" + IpContimeUtil.print(Collections.singletonList("101.132.162.159")));
    }
}
文章作者: koral
文章链接: http://luokaiii.github.io/2018/09/29/后端/java/模拟Ping获取IP响应时间/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自