Java相当于curl post请求与图像和文本 [英] Java equivalent of curl post request with image and text

查看:544
本文介绍了Java相当于curl post请求与图像和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写java代码来完成这个命令:

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

这里getImage(String)返回一个ByteArrayBody。 (我实际上是从网上下载一个图像,而不是像curl命令那样从磁盘读取,但我不认为这会有所作为)

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)

我检出了这个链接,但它不完全解决您发送的图像以及文本参数的情况:
Java等价的curl查询

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

奇怪的是,我使用几乎相同的代码HTTP post到另一个API,当我需要发送一个单独的图像没有任何文本参数的工作正常。 (无字符串体)

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)

但是当我试图发送一个图像(作为ByteArrayBody)和一些文本(作为StringBody),我得到一个神秘的错误信息服务器说的像:
'无法初始化参数对象与mimeType:application / octet-stream'。这可能是一个自定义的错误消息,所以我不能真正找到这方面很多的信息。

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.

我试过使用一些其他constuctors StringBody(),因为这一个已被弃用,但它没有帮助。不确定是什么是正确的构造函数使用,但不确定这是否会导致问题。

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.

编辑:似乎有人问过一个非常相似的问题,解决方案不适用于我:
Android:上传文件与填充POST正文在一起
我得到一个错误消息关于
失败参数初始化与mimeType:application / octet-stream)

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)

推荐答案

我没有看到它的文档,但 curl -F @ 显然试图有所帮助填写内容类型 包含 image / jpeg / code>。 HttpClient没有,默认一切都是MIME'我不知道'键入 application / octet-stream 这可能是导致您的错误。 使用设置内容类型的3-arg ctor

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)

或如果要指定filename属性, curl -F @ 也可以,但是web服务器(不像浏览器)通常不关心,提供 String 代替 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.

这篇关于Java相当于curl post请求与图像和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆