相关疑难解决方法(0)

使用HttpPost MultiPartEntityBuilder上传照片

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

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();
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

11-29 13:12:14.924:E/AndroidRuntime(15781):引起:java.lang.NoClassDefFoundError:org.apache.http.entity.ContentType 11-29 13:12:14.924:E/AndroidRuntime(15781):在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, …
Run Code Online (Sandbox Code Playgroud)

android httprequest

23
推荐指数
2
解决办法
4万
查看次数

通过HTTP POST上传文件

我想使用将文件(特定于图像)上传到REST服务器HTTP POST.我已经导入/添加到构建路径httpmime-4.3.1.jarapache-mime4j-0.6.jar.我在堆栈跟踪中得到以下错误.

这有效吗? post.setHeader("enctype", "multipart/form-data");

HTTP POST代码

public void multiPartPost() throws ClientProtocolException, IOException {
    File image = new File(EXTERNALSTORAGE + "/Pictures/sample.jpg");
    FileBody fileBody = new FileBody(image);

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    post.setHeader("enctype", "multipart/form-data");

    MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
    multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    multipartEntity.addPart("sampleImage", fileBody);
    post.setEntity(multipartEntity.build());

    HttpResponse response = client.execute(post);
    String responseBody = EntityUtils.toString(response.getEntity());
    Log.v("multiPartPost HTTP Response", responseBody);
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪

01-05 17:41:25.470: W/dalvikvm(6564): VFY: unable to resolve static field 1755 (DEFAULT_BINARY) in Lorg/apache/http/entity/ContentType;
01-05 …
Run Code Online (Sandbox Code Playgroud)

java rest http-post multipartentity

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×1

http-post ×1

httprequest ×1

java ×1

multipartentity ×1

rest ×1