curl命令java_在java中使用curl命令

I have a curl command to use

curl -s -d user.name=xxxx \

-d file=yyyy \

-d arg=-v \

'http://localhost:zzzz/templeton/v1/pig'

Can anybody tell equivalent java code for the above curl command.

Thanks in advance

解决方案

Here a example show Processbuilder that executes curl.

These section of code work fine in my environment. Actually, you will executes it with no problems. The program obtains the image from web, and save as a jpg file. The jpg file is saved at the path "/home/your_user_name/Pictures".

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Arrays;

public class ProcessBuilderTest {

public static void main(String arg[]) throws IOException {

ProcessBuilder pb = new ProcessBuilder(

"curl",

"-s",

"http://static.tumblr.com/cszmzik/RUTlyrplz/the-simpsons-season-22-episode-13-the-blue-and-the-gray.jpg ");

pb.directory(new File("/home/your_user_name/Pictures"));

pb.redirectErrorStream(true);

Process p = pb.start();

InputStream is = p.getInputStream();

FileOutputStream outputStream = new FileOutputStream(

"/home/your_user_name/Pictures/simpson_download.jpg");

String line;

BufferedInputStream bis = new BufferedInputStream(is);

byte[] bytes = new byte[100];

int numberByteReaded;

while ((numberByteReaded = bis.read(bytes, 0, 100)) != -1) {

outputStream.write(bytes, 0, numberByteReaded);

Arrays.fill(bytes, (byte) 0);

}

outputStream.flush();

outputStream.close();

}

}

For your questions. It is the most directly and intuitively to map curl to Java code, when using

Processbuilder. Just write as that:

curl -s -d user.name=xxxx \

-d file=yyyy \

-d arg=-v \

'htttp://localhost:zzzz/templeton/v1/pig'

become

ProcessBuilder pb = new ProcessBuilder("-s","-d user.name=xxxx ","-d `file=yyyy","-d rg=-v" ,"htttp://localhost:zzzz/templeton/v1/pig");`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值