小编典典

运行时CURL中的空间问题

jsp

我有一个CURL命令,需要与RuntimeJSP文件中的方法一起运行

int cnt=10;    
String strCount="count"+cnt+"";
String msg= "{'text':'"+strCount+"'}";
String cmd = "curl -X POST -H 'Content-type: application/json' --data "+ msg +" https://hooks.slack.com/services/#####/###/###";
Runtime.getRuntime().exec(cmd);

哪个工作正常,但是当我更改strCount为:

String strCount="Count is "+cnt+"";

它不再起作用了。显然,两者之间的空间Count is不再起作用。


阅读 230

收藏
2020-06-10

共1个答案

小编典典

将代码更改为此。

Runtime.getRuntime().exec(new String[] {"curl","-X", "POST","-H","'Content-type: application/json'","--data", msg, "https://hooks.slack.com/services/#####/###/###"});

根据Runtime#exec()docs的说明,如果您将各个参数设为数组,则可以传递其中带有空格的各个参数,如上所示。

2020-06-10