java post image,Java相当于curl post请求与图像和文本

I'm trying to write java code to do exactly what this command does:

curl -F "image=@image.jpg" -F "param1=some-value" -F "param2=some_value" http://example.com

I'm currently trying something like this:

HttpClient httpClient = HttpClientBuilder.create().build();

HttpPost poster = new HttpPost("http://example.com");

MultipartEntityBuilder mpEntityBuilder = MultipartEntityBuilder.create();

mpEntityBuilder.addPart("param1", new StringBody("some-value"));

mpEntityBuilder.addPart("param2", new StringBody("some-value"));

mpEntity.addPart("image", getImage("some-image"));

poster.setEntity(mpEntityBuilder .build());

HttpResponse response = httpClient.execute(poster);

here getImage(String) returns a ByteArrayBody. (I'm actually downloading an image from the web on the fly rather than reading from disk like the curl command, but I don't think this should make a difference)

I checked out this link it but it doesn't exactly address the case where you're sending both an image as well as text parameters:

Java Equivalent of curl query

What's strange is that I am using almost the same code to make an HTTP post to another API which works fine when I need to send an image alone without any textual parameters. (no stringbody)

But when I'm trying to send both an image(as a ByteArrayBody) and some text (as StringBody), I get a cryptic error message from the server that says something like:

'Could not initialize parameter object with mimeType: application/octet-stream'. It's probably a custom error message so I can't really find much information on this.

I tried using a few other constuctors for StringBody() since this one is deprecated, but it didn't help. Not sure what is the right constructor to use, but not sure if this would be causing a problem.

Edit: seems like someone's asked a very similar question before, but the solution does not work for me:

Android: upload file with filling out POST body together

I get an error message about

Failed parameter initialization with mimeType: application/octet-stream)

解决方案

I don't see it documented but curl -F @ apparently trying to be helpful fills in Content-type for some extensions/suffixes, including image/jpeg for .jpg. HttpClient does not, and defaults everything to the MIME 'I don't know' type application/octet-stream which presumably caused your error. Use the 3-arg ctor that sets the content-type

new ByteArrayBody (data, ContentType.create("image/jpeg"), null)

or if you want to specify the filename attribute, which curl -F @ also does but web servers (unlike browsers) usually don't care about, supply a String in place of the null third argument.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值