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

小智..

13

这里的示例显示了执行curl的Processbuilder.这部分代码在我的环境中运行良好.实际上,你将毫无问题地执行它.程序从Web获取图像,并保存为jpg文件.jpg文件保存在路径"/ 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");

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();

}

}

对于你的问题.使用Processbuilder时,将curl映射到Java代码是最直接和最直观的.就这样写:

curl -s -d user.name=xxxx \

-d file=yyyy \

-d arg=-v \

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

成为

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值