【问题标题】:Upload Photo using HttpPost MultiPartEntityBuilder使用 HttpPost MultiPartEntityBuilder 上传照片
【发布时间】:2013-11-29 11:19:19
【问题描述】:

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

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):由以下原因引起: 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): 在 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): 由: java.lang.NoClassDefFoundError:org.apache.http.util.Args 11-29 14:00:33.364:E/AndroidRuntime(19478):在 org.apache.http.entity.mime.content.AbstractContentBody.(AbstractContentBody.java:48) 11-29 14:00:33.364: E/AndroidRuntime(19478): 在 org.apache.http.entity.mime.content.InputStreamBody.(InputStreamBody.java:69) 11-29 14:00:33.364: E/AndroidRuntime(19478): 在 org.apache.http.entity.mime.content.InputStreamBody.(InputStreamBody.java:62) 11-29 14:00:33.364: E/AndroidRuntime(19478): 在 com.petcial.petopen.custom.JSONParser.makePostFileRequest(JSONParser.java:233)

这些库解决了我的问题:

【问题讨论】:

  • FileBody 不属于标准 Android 库。你用的是什么罐子?
  • 你在项目中添加了httpmime-4.3.jar吗?
  • 当然。在 libs 文件夹中
  • 您使用的是什么 IDE?蚀?如果是这样,请确保已添加并导出它,as in this answer
  • 嗨,@FilipLuch 你找到解决这个问题的办法了吗?

标签: android httprequest


【解决方案1】:

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

【讨论】:

  • 我做到了。在第 221 行仍然出现同样的错误。也许代码有问题?
  • 感谢您的帮助。我更新了我的问题,因为我的项目还需要一个库。
【解决方案2】:

有我的工作解决方案,使用 apache http 库发送带有帖子的图像(这里非常重要的是边界添加它在我的连接中没有它就无法工作):

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();

HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(StaticData.AMBAJE_SERVER_URL + StaticData.AMBAJE_ADD_AMBAJ_TO_GROUP);

String boundary = "-------------" + System.currentTimeMillis();

httpPost.setHeader("Content-type", "multipart/form-data; boundary="+boundary);

ByteArrayBody bab = new ByteArrayBody(imageBytes, "pic.png");
StringBody sbOwner = new StringBody(StaticData.loggedUserId, ContentType.TEXT_PLAIN);
StringBody sbGroup = new StringBody("group", ContentType.TEXT_PLAIN);

HttpEntity entity = MultipartEntityBuilder.create()
                    .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                    .setBoundary(boundary)
                    .addPart("group", sbGroup)
                    .addPart("owner", sbOwner)
                    .addPart("image", bab)
                    .build();

httpPost.setEntity(entity);

try {
       HttpResponse response = httpclient.execute(httpPost);
       ...then reading response

【讨论】:

  • 你让我免于失去另一天!!我在 Stackoverflow 周围看了几个小时,添加边界后它开始工作了!!谢谢你
【解决方案3】:

最好传递图像文件的路径。下面是我用来将图像上传到服务器的代码。

public class UploadProductDetails {


    public void uploadProductDetails(String filePath, String fileName)
    {

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

                HttpClient httpClient = new DefaultHttpClient();

                httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
                        System.getProperty("http.agent"));



                HttpPost httpPost = new HttpPost("http://ipaddress");


                InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), "abc.png");
                MultipartEntity multipartEntity = new MultipartEntity();
                multipartEntity.addPart("file", inputStreamBody);


                httpPost.setEntity(multipartEntity);
                HttpResponse httpResponse = httpClient.execute(httpPost);

                // Handle response back from script.
                if(httpResponse != null) {
                    //Toast.makeText(getBaseContext(),  "Upload Completed. ", 2000).show();

                } else { // Error, no response.
                    //Toast.makeText(getBaseContext(),  "Server Error. ", 2000).show();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
    }

}

【讨论】:

  • 如果您想给出第二个答案或意见,请将此答案添加到您的第一个答案并进行编辑..
  • 我在 InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), "abc.png"); 上的类型 org.apache.http.entity.ContentType cannot be resolved. It is indirectly referenced from required .class files 上出现错误
  • 您可以使用这些链接来获取您的解决方案。
  • 我添加了所需的库。但我仍然遇到同样的错误。可能是什么问题?请检查更新的问题
【解决方案4】:

请注意,使用此代码:new InputStreamBody(new ByteArrayInputStream(yourByteArray)) 可能会导致一些问题,因为它在调用 getContentLength() 时会返回 -1

改为使用:new ByteArrayBody(yourByteArray)

【讨论】:

    猜你喜欢
    • 2016-02-29
    • 2014-07-11
    • 2014-12-10
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多