如何在执行之前获取processbuilder命令

时间:2017-05-18 07:17:38

标签: java linux curl processbuilder

我想知道在它发生之前将执行的命令。

      String cmd[] = {"curl",
            "-X",
            "POST",
            "https://api.renam.cl/medicion/insert?access-token={Yoq3UGQqDKP4D1L3Y6xIYp-Lb6fyvavpF3Lm-8cD}",
            "-H",
            "content-type: application/json",
            "-d",
            json.toString()};

        ProcessBuilder pb = new ProcessBuilder(cmd);

        Log.debug("COMANDO.TOSTRING " + pb.command().toString());

        Process p = pb.start();

        Log.debug(p.getOutputStream().toString());

        p.waitFor();

        BufferedReader reader
                = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String readline;

        while ((readline = reader.readLine()) != null) {
            Log.debug(readline);
        }

使用readline我有服务器的答案输出,但我不知道很热,以获得我已经与processbuilder一起完成的curl命令。

编辑1:

我只需要使用linux控制台发送此命令:

  

curl -X POST' https://api.com/data/insert?access-token=Yoq3UGQqDKP4D1L3Y6xIYp-Lb6fyvavpF3Lm-8cD' -H' content-type:application / json' -d' {" pm25":2," timestamp":1495077872," dispositivo_mac":" 12:34:56:78: 90:12" }'

基本上我需要打印由ProcessBuilder对象处理的cmd数组,以便在执行星形方法之前查看它。

1 个答案:

答案 0 :(得分:0)

以下是用于打印可运行命令的代码:

private String getRunnableCommand(ProcessBuilder processBuilder)
{
    val commandsList = processBuilder.command();
    val runnableCommandBuilder = new StringBuilder();
    var commandIndex = 0;
    for (val command : commandsList)
    {
        if (command.contains(" "))
        {
            runnableCommandBuilder.append("\"");
        }
        runnableCommandBuilder.append(command);

        if (command.contains(" "))
        {
            runnableCommandBuilder.append("\"");
        }

        if (commandIndex != commandsList.size() - 1)
        {
            runnableCommandBuilder.append(" ");
        }

        commandIndex++;
    }
    return runnableCommandBuilder.toString();
}

varval来自Lombok,使用以下maven依赖项:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>LATEST</version>
    <scope>provided</scope>
</dependency>