5

I want to ping a target IP address and receive a response. To achieve this, I'm using windows command line in Java with runtime.exec method and process class. I'm getting the response using inputStreamReader.

My default charset is windows-1254, it's Turkish. When I receive it, the response contains Turkish characters but Turkish characters are not displayed correctly in the console.

I want to get a numeric value from the response I get but the value that I am searching for contains some Turkish characters, so when I look it up, I can't find it.

The codes are below, what I need to know is how to get the Turkish characters visible here:

runtime = Runtime.getRuntime();
process = runtime.exec(pingCommand);

BufferedReader bReader = new BufferedReader(
        new InputStreamReader(process.getInputStream(), "UTF8"));

String inputLine;
while ((inputLine = bReader.readLine()) != null) {
    pingResult += inputLine;
}

bReader.close();
process.destroy();

System.out.println(pingResult);
4

6 回答 6

6

为了解决这个问题,检查当前操作系统在其命令行上使用的字符集并获取与该字符集兼容的数据是必要的。

我发现土耳其语 XP 命令行的字符集是 CP857,当你像下面这样编辑代码时,问题就解决了。

BufferedReader bReader = new BufferedReader(
        new InputStreamReader(process.getInputStream(), "CP857"));

谢谢你的帮助。

注意:您可以通过“chcp”命令了解您的默认命令行字符集。

于 2011-05-04T18:53:17.017 回答
2

Are you saying the characters are being retrieved properly but System.out isn't printing them right? System.out is definitely an OLD class. Maybe try

PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out,"charactersethere"));
out.println(“encoded system”);
out.flush();
out.close();

That MIGHT work

于 2011-05-01T17:53:33.347 回答
2

如果您只需要 ping 并获取响应时间,那么从控制台读取输出可能会过大。只要您使用的是 Java5 或更新版本,就有一种更简单的方法:

这是一个完整的程序,您可以使用它来执行此操作。注意:在 Unix/Linux/Mac OS 上,您必须在“sudo”下运行此程序才能从“localhost”以外的任何地方获得响应。

import java.net.InetAddress;
import java.io.IOException;

class PingTest {

  public static void main(String[] args) {
    try {
      String hostnameOrIP = args[0];
      int timeout = Integer.parseInt(args[1]);
      int pingCount = Integer.parseInt(args[2]);

      System.out.println("Pinging '" + hostnameOrIP + "'");
      for (int i = 0; i < pingCount; i++) {
        System.out.println("Response time: " + getPingResponseTime(hostnameOrIP, timeout));
      }
    } catch (Exception e) {
      System.out.println("Usage: java PingTest <hostname/IP address> <timeout in milliseconds> <number of pings to send>\n");
    }
  }

  static long getPingResponseTime(String hostnameOrIP, int timeout) {
      long startTime = System.currentTimeMillis();

      boolean successfulPing = false;

      try {
        successfulPing = InetAddress.getByName(hostnameOrIP).isReachable(timeout);
      } catch (IOException ioe) {
        successfulPing = false;
      }

      long endTime = System.currentTimeMillis();

      long responseTime = endTime-startTime;

      if (successfulPing == false)
        responseTime = -1;

      return responseTime;
  }

}

以下是我在 Mac OS 上运行以下命令时得到的结果(结果以毫秒为单位):

$ sudo java PingTest google.com 5000 5
Pinging 'google.com'
Response time: 419
Response time: 15
Response time: 15
Response time: 16
Response time: 16

响应时间可能因运行而异,但我看到大多数主要站点的响应时间都小于 20 毫秒,尤其是在您运行多个 ping 时

于 2011-05-01T17:47:40.450 回答
1

System.out(...) - 和 Java 控制台 - 在编码方面非常有限。您可以期望基本的 ASCII 字符能够正常工作,仅此而已。如果您想使用任何其他编码,那么您应该将输出写入文本文件或 GUI。如果你写到控制台,你总是不得不应对各种非 ASCII 字符的糟糕处理。

于 2011-05-01T18:55:12.183 回答
0

您需要在首选项下更改 Eclipse 默认编码设置。可以设置为UTF8

于 2011-07-02T12:14:08.230 回答
0

试试这个命令行命令:

chcp

我的命令行回答 866 所以我使用 CP866

于 2013-06-05T10:07:59.497 回答