Using Curl in Java – 在Java中使用Curl

最后修改: 2019年 1月 14日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’re going to look at how to use the curl tool inside a Java program.

在本教程中,我们将看看如何在Java程序中使用curl工具。

Curl is a networking tool used to transfer data between a server and the curl client using protocols like HTTP, FTP, TELNET, and SCP.

Curl是一种网络工具,用于在服务器和curl客户端之间传输数据,使用的协议包括HTTP、FTP、TELNET和SCP。

2. Basic Use of Curl

2.卷曲的基本使用方法

We can execute curl commands from Java by using the ProcessBuilder — a helper class for building instances of the Process class.

我们可以通过使用ProcessBuilder–一个用于构建Process类实例的辅助类,从Java中执行curl命令。

Let’s see an example of sending commands directly to the operating system:

让我们看一个直接向操作系统发送命令的例子。

String command =
  "curl -X GET https://postman-echo.com/get?foo1=bar1&foo2=bar2";
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));

First, we create the command variable before passing it to the ProcessBuilder constructor.

首先,我们创建command变量,然后将其传递给ProcessBuilder构造函数。

It’s worth noting here that if the curl executable isn’t on our system path, we’ll have to provide its full path in our command string.

这里值得注意的是,如果curl可执行文件不在我们的系统路径上,我们必须在命令字符串中提供它的完整路径。

We can then set the working directory for the ProcessBuilder and start the process:

然后我们可以为ProcessBuilder设置工作目录,并启动进程。

processBuilder.directory(new File("/home/"));
Process process = processBuilder.start();

From here on, we can get the InputStream by accessing it from the Process instance:

从这里开始,我们可以通过从Process实例访问它来获得InputStream

InputStream inputStream = process.getInputStream();

When the processing is complete, we can get the exit code with:

当处理完成后,我们可以通过以下方式获得退出代码。

int exitCode = process.exitValue();

If we need to run additional commands, we can reuse the ProcessBuilder instance by passing new commands and arguments in a String array:

如果我们需要运行额外的命令,我们可以重用ProcessBuilder实例,在String数组中传递新的命令和参数:

processBuilder.command(
  new String[]{"curl", "-X", "GET", "https://postman-echo.com?foo=bar"});

Finally, to terminate each Process instance, we should use:

最后,为了终止每个Process实例,我们应该使用。

process.destroy();

3. A Simple Alternative to the ProcessBuilder

3.一个替代ProcessBuilder的简单方法

As an alternative to using the ProcessBuilder class, we can use Runtime.getRuntime() to get an instance of the Process class.

作为使用ProcessBuilder类的替代方法,我们可以使用Runtime.getRuntime()来获取Process类的一个实例。

Let’s see another sample curl command – this time using a POST request:

让我们看看另一个curl命令的例子–这次是使用POST请求。

curl -X POST https://postman-echo.com/post --data foo1=bar1&foo2=bar2

Now, let’s execute the command by using the Runtime.getRuntime() method:

现在,让我们通过使用Runtime.getRuntime()方法来执行这个命令。

String command = "curl -X POST https://postman-echo.com/post --data foo1=bar1&foo2=bar2";
Process process = Runtime.getRuntime().exec(command);

Firstly, we create an instance of the Process class again, but this time using Runtime.getRuntime(). We can get an InputStream as in our previous example by calling the getInputStream() method:

首先,我们再次创建一个Process类的实例,但这次是使用Runtime.getRuntime()。我们可以通过调用getInputStream()方法得到一个InputStream,就像我们之前的例子一样。

process.getInputStream();

When the instance is no longer needed, we should release system resources by calling the destroy() method.

当不再需要这个实例时,我们应该通过调用destroy()方法释放系统资源。

4. Conclusion

4.结论

In this article, we have shown two ways of using curl in Java.

在这篇文章中,我们展示了在Java中使用curl的两种方法。

This and more code examples are available over on GitHub.

这和更多的代码实例可在GitHub上获得