Android Multipart上传

时间:2010-10-12 22:20:35

标签: android http-status-code-404 multipart

作为我的Android应用程序的一部分,我想上传要远程存储的位图。我有简单的HTTP GET和POST通信工作,但有关如何进行多部分POST的文档似乎与独角兽一样罕见。

此外,我想直接从内存传输图像,而不是使用文件。在下面的示例代码中,我从一个文件中获取一个字节数组,以便稍后使用HttpClient和MultipartEntity。

    File input = new File("climb.jpg");
    byte[] data = new byte[(int)input.length()];
    FileInputStream fis = new FileInputStream(input);
    fis.read(data);

    ByteArrayPartSource baps = new ByteArrayPartSource(input.getName(), data);

这一切对我来说都是相当清楚的,除了我不能为我的生活找到从哪里得到这个ByteArrayPartSource。我已链接到httpclient和httpmime JAR文件,但没有骰子。我听说HttpClient 3.x和4.x之间的包结构发生了巨大的变化。

是否有人在Android中使用此ByteArrayPartSource,以及它们是如何导入的?

在浏览文档并搜索互联网之后,我想出了一些符合我需求的东西。要创建一个多部分请求,例如表单POST,以下代码为我做了诀窍:

    File input = new File("climb.jpg");

    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost:3000/routes");
    MultipartEntity multi = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    String line;

    multi.addPart("name", new StringBody("test"));
    multi.addPart("grade", new StringBody("test"));
    multi.addPart("quality", new StringBody("test"));
    multi.addPart("latitude", new StringBody("40.74"));
    multi.addPart("longitude", new StringBody("40.74"));
    multi.addPart("photo", new FileBody(input));
    post.setEntity(multi);

    HttpResponse resp = client.execute(post);

HTTPMultipartMode.BROWSER_COMPATIBLE位非常重要。感谢Radomir's blog就此问题。

3 个答案:

答案 0 :(得分:2)

试试这个:

 HttpClient httpClient = new DefaultHttpClient() ;

 HttpPost httpPost = new HttpPost("http://example.com");
 MultipartEntity entity = new MultipartEntity();     
 entity.addPart("file", new FileBody(file));
 httpPost.setEntity(entity );
 HttpResponse response = null;

 try {
     response = httpClient.execute(httpPost);
 } catch (ClientProtocolException e) {
     Log.e("ClientProtocolException : "+e, e.getMessage());         
 } catch (IOException e) {
     Log.e("IOException : "+e, e.getMessage());

 } 

答案 1 :(得分:1)

也许您可以执行以下步骤将库导入Android。

需求库    - apache-mime4j-0.6.jar    - httpmime-4.0.1.jar

  1. 右键单击您的项目,然后单击属性
  2. 选择java构建路径
  3. 选择名为“订单和导出”的标签
  4. 应用
  5. 由于现有的apk不适合新库
  6. ,因此使用adb uninstall完全卸载您的apk文件
  7. 再次安装你的apk
  8. 运行它
  9. 谢谢,

    Jenz

答案 2 :(得分:0)

我遇到了同样的问题。我正在尝试通过MultiPart实体上传图像,并且它发现HttpClient / MIME上的几个更新正在破解所有内容。我正在尝试以下代码,出现错误“ NoClassDefFoundError ”:

public static void executeMultipartPost(File image, ArrayList<Cookie> cookies, String myUrlToPost) {
    try {
        // my post instance
        HttpPost httppost = new HttpPost(myUrlToPost);
        // setting cookies for the connection session
        if (cookies != null && cookies.size() > 0) {
            String cookieString = "";
            for (int i=0; i<cookies.size(); ++i) {
                cookieString += cookies.get(i).getName()+"="+cookies.get(i).getValue()+";";
            }
            cookieString += "domain=" + BaseUrl + "; " + "path=/";
            httppost.addHeader("Cookie", cookieString);
        }
        // creating the http client
        HttpClient httpclient = new DefaultHttpClient();
        // creating the multientity part [ERROR OCCURS IN THIS BELLOW LINE]
        MultipartEntity multipartEntity = new MultipartEntity();
        multipartEntity.addPart("photoupload", new FileBody(image));
        httppost.setEntity(multipartEntity);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
    } catch (Exception e) {}
}

此方法完全可编译并使用httpclient-4.0.1.jarhttpmime-4.2.jar库,但我再次记得它在评论行中崩溃了。

相关问题