gpt4 book ai didi

android - 使用 HttpPost MultiPartEntityBuilder 上传照片

转载 作者:IT老高 更新时间:2023-10-28 22:22:41 30 4
gpt4 key购买 nike

我正在尝试将拍摄的照片上传到服务器。这就是我所做的:

public JSONObject makePostFileRequest(String url, String photoFile) {
try {
// photoFile = /path/tofile/pic.jpg
DefaultHttpClient httpClient = GlobalData.httpClient;
HttpPost httpPost = new HttpPost(url);

File file = new File(photoFile);
FileBody fileBody = new FileBody(file); // here is line 221

MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();

multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("PhotoMessage", fileBody);

httpPost.setEntity(multipartEntity.build());

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

我收到此错误:

11-29 13:12:14.924: E/AndroidRuntime(15781): Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType 11-29 13:12:14.924: E/AndroidRuntime(15781): at org.apache.http.entity.mime.content.FileBody.(FileBody.java:89) 11-29 13:12:14.924: E/AndroidRuntime(15781): at com.petcial.petopen.custom.JSONParser.makePostFileRequest(JSONParser.java:221)

我做错了什么?


更新

InputStream inputStream;
inputStream = new FileInputStream(new File(photoFile));
byte[] data;
data = IOUtils.toByteArray(inputStream);

httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
System.getProperty("http.agent"));
InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), "Pic.jpg");

MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();

multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("PhotoMessage", inputStreamBody);

httpPost.setEntity(multipartEntity.build());

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

这是错误:

11-29 14:00:33.364: E/AndroidRuntime(19478): Caused by: java.lang.NoClassDefFoundError: org.apache.http.util.Args 11-29 14:00:33.364: E/AndroidRuntime(19478): at org.apache.http.entity.mime.content.AbstractContentBody.(AbstractContentBody.java:48) 11-29 14:00:33.364: E/AndroidRuntime(19478): at org.apache.http.entity.mime.content.InputStreamBody.(InputStreamBody.java:69) 11-29 14:00:33.364: E/AndroidRuntime(19478): at org.apache.http.entity.mime.content.InputStreamBody.(InputStreamBody.java:62) 11-29 14:00:33.364: E/AndroidRuntime(19478): at com.petcial.petopen.custom.JSONParser.makePostFileRequest(JSONParser.java:233)

这些库解决了我的问题:

最佳答案

enter image description here在 Order and Export 选项卡中检查该 jar 并运行。

关于android - 使用 HttpPost MultiPartEntityBuilder 上传照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20284542/

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