- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试将拍摄的照片上传到服务器。这就是我所做的:
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)
这些库解决了我的问题:
最佳答案
在 Order and Export 选项卡中检查该 jar 并运行。
关于android - 使用 HttpPost MultiPartEntityBuilder 上传照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20284542/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!