gpt4 book ai didi

java - 我可以使用 UrlEncodedFormEntity 为多个部分上传图像和文本吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:39:42 27 4
gpt4 key购买 nike

图像通常需要特殊的 HTTP header ,例如:

Content-disposition: attachment; filename="file2.jpeg"
Content-type: image/jpeg
Content-Transfer-Encoding: binary

我正在使用以下方法构建我的 POST:

List<NameValuePair> formparams = new ArrayList<NameValuePair>();
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams);

urlEncodedFormEntity 允许设置内容类型,但我不知道如何混合图像和文本??

最佳答案

try {
File file = new File(Environment.getExternalStorageDirectory(),"FMS_photo.jpg");

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://homepage.com/path");
FileBody bin = new FileBody(file);


Charset chars = Charset.forName("UTF-8");

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("problem[photos_attributes][0][image]", bin);
reqEntity.addPart("myString", new StringBody("17", chars));

post.setEntity(reqEntity);
HttpResponse response = client.execute(post);

HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
resEntity.consumeContent();
}

return true;

} catch (Exception ex) {

globalStatus = UPLOAD_ERROR;
serverResponse = "";
return false;
} finally {

}

在此问题属性中将携带图像,而 myString 携带字符串...

关于java - 我可以使用 UrlEncodedFormEntity 为多个部分上传图像和文本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6797057/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com