Runtime.exec() 的行为不符合预期
所以我有一个我想要执行的字符串,一个curl字符串...当它被执行时,它正在屠宰我的用户代理字符串...
这是我正在执行的字符串...
/usr/bin/curl -L --no-keepalive --max-time 30 --connect-timeout 30 --insecure --max-redirs 10 --stderr /var/folders/+j/+jqu+V1eEoSalBbXTff74U+++TI/-Tmp-/output7756019899402490058.tmp --cookie-jar /var/folders/+j/+jqu+V1eEoSalBbXTff74U+++TI/-Tmp-/cookies4551380191209065239.tmp --user-agent "1 2 3 4 5" --dump-header /var/folders/+j/+jqu+V1eEoSalBbXTff74U+++TI/-Tmp-/headers159122813500476027.tmp http://test.com
这是我的代码使用来执行它
Process pr = null;
Runtime run = Runtime.getRuntime();
try {
pr = run.exec(cmdline.split(" "));
A ret = f.f(pr);
pr.waitFor();
return ret;
} catch (Exception ex) {
throw new RuntimeException("Executing " + cmdline, ex);
} finally {
try {
// close all those bloody streams
pr.getErrorStream().close();
pr.getInputStream().close();
pr.getOutputStream().close();
} catch (IOException ex) {
Log.get().exception(Log.Level.Error, "Closing stream: ", ex);
}
}
这是 apache 日志,用户代理搞乱了...
192.168.1.105 - - [07/Feb/2012:20:59:38 -0500] "GET / HTTP/1.1" 200 6791 "-" "\"1"
apache 中的预期结果应该显示完整的用户代理(在本例中为 1 2 3 4 5)
192.168.1.105 - - [07/Feb/2012:20:59:38 -0500] "GET / HTTP/1.1" 200 6791 "-" "1 2 3 4 5"
So I have a string I want to exec, a curl string... when it gets exec'd it is butchering my user-agent string...
Here is the string I am exec'ing...
/usr/bin/curl -L --no-keepalive --max-time 30 --connect-timeout 30 --insecure --max-redirs 10 --stderr /var/folders/+j/+jqu+V1eEoSalBbXTff74U+++TI/-Tmp-/output7756019899402490058.tmp --cookie-jar /var/folders/+j/+jqu+V1eEoSalBbXTff74U+++TI/-Tmp-/cookies4551380191209065239.tmp --user-agent "1 2 3 4 5" --dump-header /var/folders/+j/+jqu+V1eEoSalBbXTff74U+++TI/-Tmp-/headers159122813500476027.tmp http://test.com
Here is the code I use to exec it
Process pr = null;
Runtime run = Runtime.getRuntime();
try {
pr = run.exec(cmdline.split(" "));
A ret = f.f(pr);
pr.waitFor();
return ret;
} catch (Exception ex) {
throw new RuntimeException("Executing " + cmdline, ex);
} finally {
try {
// close all those bloody streams
pr.getErrorStream().close();
pr.getInputStream().close();
pr.getOutputStream().close();
} catch (IOException ex) {
Log.get().exception(Log.Level.Error, "Closing stream: ", ex);
}
}
Here is the apache logs with the user-agent messed up...
192.168.1.105 - - [07/Feb/2012:20:59:38 -0500] "GET / HTTP/1.1" 200 6791 "-" "\"1"
The expected result in apache should show the FULL user agent (in this case 1 2 3 4 5)
192.168.1.105 - - [07/Feb/2012:20:59:38 -0500] "GET / HTTP/1.1" 200 6791 "-" "1 2 3 4 5"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在空格上进行了拆分,并且“1 2 3 4 5”中有空格。
You're splitting on spaces, and "1 2 3 4 5" has spaces in it.
我建议以不同的方式传递它。我会使用分号 (;) 或任何非易失性分隔符。并以这种方式分割字符串。这里要记住的是,您并不关心传递到程序中的内容,只关心您愿意执行的内容。因此,您的 cmdLine 变量应如下所示:
--user-agent; “1 2 3 4 5”; --dump-header;
根据需要使用 String.trim()。
I recommend passing it in delimited differently. I would use semicolon (;) or any non-volatile delimiter. and split the string that way. The thing to remember here is you do not care about what gets passed into your program only what you are willing to execute. Therefore your cmdLine variable should look like this:
--user-agent; "1 2 3 4 5"; --dump-header;
use String.trim() as necessary.