10

作为我的 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,他们是如何导入它的?

在搜索文档并搜索 Internet 之后,我想出了一些适合我需要的东西。要发出诸如表单 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关于这个的博客。

4

3 回答 3

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());

 } 
于 2010-11-04T18:42:44.520 回答
1

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

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

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

谢谢,

詹兹

于 2012-09-26T18:28:10.220 回答
0

我有同样的问题。我正在尝试通过 MultiPart Entity 上传图像,它发现 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库,但我记得它在我的注释行中崩溃了。

于 2012-06-05T16:16:11.203 回答