在 java 中对本地主机执行 curl 命令

Execute a curl command in java to a localhost

我是初学者,我需要执行 docker 命令而不使用 ubuntu 中的 sudo 来使用 gcc 编译器。 为了实现这一点,我的一个朋友在 go lang 中编写了一个脚本,名为 http-to-shell,这是一个在 http://localhost:4000 上作为 http 服务器侦听的程序,并在 http://localhost:4000 上执行命令终端和 returns 作为 json 响应输出到客户端。

我运行命令

sudo go run http-to-shell.go 

然后在另一个选项卡上我使用

这样的命令
curl -d command=ls http://localhost:4000

这很好用,然后如果我使用

curl --data-urlencode "command@command.txt" http://localhost:4000

它 运行 是 command.txt 中的 docker 命令,并在我 运行 sudo 的前一个选项卡上显示程序的输出去运行。

现在,我的主要问题。由于我是初学者,我不知道如何从 ubuntu.

中 localhost:8080 的 jsp 页面 运行

我一直在尝试,我尝试使用Using curl command in java中的答案 但流程构建器不起作用。 我需要 运行 这个 curl 命令并得到 json 响应来向用户显示他的代码的输出。

请帮帮我。

如果您想在代码中使用 json 的结果,那么 curl 可能不是您想要的。然而,如果您想从命令行执行操作,Curl 非常棒,是的,有 Java curl 绑定,但我个人会使用 Apache HttpClient

下面是一个使用 HttpClient 进行 REST 调用的示例。 http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-apache-httpclient/

该示例使用 Spring 作为依赖项,我不确定您使用的是什么,因此您可能必须自己下载 jar 并将其放在您的 class 路径中