本文介绍了通过上传照片HttpPost MultiPartEntityBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想上传拍摄的照片到服务器。这是我做的:
I am trying to upload the taken photo to server. this is what i do:
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();
我得到这个错误:
I get this error:
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.<init>(FileBody.java:89)
11-29 13:12:14.924: E/AndroidRuntime(15781): at com.petcial.petopen.custom.JSONParser.makePostFileRequest(JSONParser.java:221)
我是什么做错了吗?
What am I doing wrong?
更新
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.<init>(AbstractContentBody.java:48)
11-29 14:00:33.364: E/AndroidRuntime(19478): at org.apache.http.entity.mime.content.InputStreamBody.<init>(InputStreamBody.java:69)
11-29 14:00:33.364: E/AndroidRuntime(19478): at org.apache.http.entity.mime.content.InputStreamBody.<init>(InputStreamBody.java:62)
11-29 14:00:33.364: E/AndroidRuntime(19478): at com.petcial.petopen.custom.JSONParser.makePostFileRequest(JSONParser.java:233)
这些库解决了我的问题:
these libraries solved my issue:
推荐答案
检查罐子的排序和导出选项卡,并运行。
Check that jar in Order and Export tab and run.
这篇关于通过上传照片HttpPost MultiPartEntityBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!