如何在java中使用curl下载zip文件

时间:2021-07-15 15:26:47

I am trying to download a zip file using curl in java

我试图在java中使用curl下载一个zip文件

public static void main(String[] args) throws Exception {

     String url = "https://abcd.com ";
     String[] command = {
              "curl", 
              " -X ", "POST ", 
              "-H ", "\"Content-Type: application/json\" ", 
              "-H ","Accept:application/json ",
              "-H ", "Accept-Encoding:gzip ", 
              "-d' ", url,
              " >> "," /Users/myUser/Documents/Test1.gzip"};

     Process p = Runtime.getRuntime().exec(command);
     p.exitValue();

}

However nothing is getting triggered . Please let me know if I have missed anything .

然而,什么都没有被触发。如果我遗漏了什么,请告诉我。

When I am running the same command from terminal, the zip file is getting downloaded .

当我从终端运行相同的命令时,zip文件正在下载。

2 个解决方案

#1


1  

" >> "

Runtime.exec() does not execute a shell, and the shell is the only thing that understands >>. If you want redirection, use the redirection features of ProcessBuilder, or adjust your command to start a shell.

Runtime.exec()不执行shell,而shell是唯一理解>>的东西。如果要重定向,请使用ProcessBuilder的重定向功能,或调整命令以启动shell。

Notes:

  1. Difficult to understand why you're using Java at all here, rather than a shell script, or conversely if you must use Java why you're using curl. You don't need to keep a dog and bark yourself.

    很难理解为什么你在这里使用Java而不是shell脚本,或者相反,如果你必须使用Java,为什么你使用curl。你不需要养狗和自己吠叫。

  2. Not much point in calling exitValue() unless you're going to do something with the result, or take some notice of the fact that the process hasn't exited yet.

    调用exitValue()没什么意义,除非你要对结果做些什么,或者注意一下这个过程还没有退出的事实。

#2


0  

This is as simple as writing a file Use apache commons-io. It has a FileUtils library that does all the work under the hood.

这就像编写文件一样简单使用apache commons-io。它有一个FileUtils库,可以完成所有工作。

Essentially, after to curl the file, you need to save it to the disk (FileOutputStream) on some well defined path.

基本上,在卷曲文件之后,您需要将其保存到某个定义良好的路径上的磁盘(FileOutputStream)。

reference(also has code snippet in accepted solution): How to download and save a file from Internet using Java?

引用(在接受的解决方案中也有代码片段):如何使用Java从Internet下载和保存文件?

#1


1  

" >> "

Runtime.exec() does not execute a shell, and the shell is the only thing that understands >>. If you want redirection, use the redirection features of ProcessBuilder, or adjust your command to start a shell.

Runtime.exec()不执行shell,而shell是唯一理解>>的东西。如果要重定向,请使用ProcessBuilder的重定向功能,或调整命令以启动shell。

Notes:

  1. Difficult to understand why you're using Java at all here, rather than a shell script, or conversely if you must use Java why you're using curl. You don't need to keep a dog and bark yourself.

    很难理解为什么你在这里使用Java而不是shell脚本,或者相反,如果你必须使用Java,为什么你使用curl。你不需要养狗和自己吠叫。

  2. Not much point in calling exitValue() unless you're going to do something with the result, or take some notice of the fact that the process hasn't exited yet.

    调用exitValue()没什么意义,除非你要对结果做些什么,或者注意一下这个过程还没有退出的事实。

#2


0  

This is as simple as writing a file Use apache commons-io. It has a FileUtils library that does all the work under the hood.

这就像编写文件一样简单使用apache commons-io。它有一个FileUtils库,可以完成所有工作。

Essentially, after to curl the file, you need to save it to the disk (FileOutputStream) on some well defined path.

基本上,在卷曲文件之后,您需要将其保存到某个定义良好的路径上的磁盘(FileOutputStream)。

reference(also has code snippet in accepted solution): How to download and save a file from Internet using Java?

引用(在接受的解决方案中也有代码片段):如何使用Java从Internet下载和保存文件?