【问题标题】:Android Multipart HTTP Post does not send the File's MIME typeAndroid Multipart HTTP Post 不发送文件的 MIME 类型
【发布时间】:2011-03-29 19:14:05
【问题描述】:

试图找出我的编码有什么问题。我关注了here 的一篇博文。

我设法获得了将文件实际上传到 PHP Web 服务的代码。然而,由于某种原因,虽然我已经明确设置了文件的 MIME 类型,但 PHP 显示 MIME 只是一个空白字符串,因此被拒绝了。

这是我的编码:

public String SendPost(String fn, String bid, String caption, String uid, String APIKey, String postHash) 
        throws ParseException, ClientProtocolException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    HttpPost httppost = new HttpPost(UrbURL);

    Log.i("POSTFN", fn);
    Log.i("POSTFN", bid);
    Log.i("POSTFN", caption);
    Log.i("POSTFN", uid);
    Log.i("POSTFN", APIKey);
    Log.i("POSTFN", postHash);

    String postAuth = uid + postHash;
    postAuth = md5(postAuth);
    postAuth = postAuth.substring(0, 16);
    //Log.i("POSTAUTH", postAuth);

    MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    /*File tempImg = new File(fn);
    FileBody bin = new FileBody(tempImg, "image/jpg");*/
    mp.addPart("business_photo", new FileBody(new File(fn), "image/jpg"));

    //StringBody s = new StringBody(bid, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("business_id", new StringBody(bid, "text/plain", Charset.forName( "UTF-8" )));

    //s = new StringBody(caption, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("photo_caption", new StringBody(caption, "text/plain", Charset.forName( "UTF-8" )));

    //s = new StringBody(uid, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("user_id", new StringBody(uid, "text/plain", Charset.forName( "UTF-8" )));

    //s = new StringBody(APIKey, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("apikey", new StringBody(APIKey, "text/plain", Charset.forName( "UTF-8" )));

    //s = new StringBody(postAuth, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("auth", new StringBody(postAuth, "text/plain", Charset.forName( "UTF-8" )));

    httppost.setEntity(mp);

    String response = EntityUtils.toString( httpclient.execute( httppost ).getEntity(), "UTF-8" );

    httpclient.getConnectionManager().shutdown();

    return response;
}

非常感谢之前:)

【问题讨论】:

  • 嗨,你能告诉我什么是'charset',我设置了什么

标签: android httpclient http-post mime-types multipartform-data


【解决方案1】:

我遇到了同样的问题,刚刚解决了。

我发现在使用ByteArrayBody 图像时,使用HttpMultipartMode.BROWSER_COMPATIBLE 会阻止在我的请求中设置正确的mimeType。我认为这可能与您遇到的问题相同。

当我改变这一行时:

MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

MultipartEntity mp = new MultipartEntity();

然后 MIME 类型设置正确并且服务上传工作正常。

我看到人们使用BROWSER_COMPATIBLE 来解决另一个问题,但希望你不需要它。

【讨论】:

  • 这对我有帮助。非常感谢!
【解决方案2】:

在将 HttpClient 从 4.1 版更新到 4.4 版时,我也遇到了同样的问题。既不删除HttpMultipartMode.BROWSER_COMPATIBLE 用其他替代选项替换也没有帮助。

对我来说需要最小更改的解决方案是在构造FileBody 时明确指定filename,即替换

new FileBody(file, mimeType)

new FileBody(file, filename, mimeType, charset)

对于 HttpClient 4.4 版本,原因可能是new FileBody(file, mimeType)null 分配给filename 字段,而不是使用file.getName()。然后,在HttpMultipart 中执行formatMultipartHeader 时,在BROWSER_COMPATIBLE 模式下检查将要格式化的部分的filename 如果不为空。如果为 null,则忽略整个主体。

【讨论】:

    【解决方案3】:

    我在发布 3 个 FileBodies 和 1 个 mime 信息时遇到了同样的问题,除非我只发送一个文件体,否则从未收到 mime(以 json 格式)。我总是先添加文件体,这导致了我的问题。我在一开始就通过 addpart mime 颠倒了顺序,并且在 php 服务器端正确接收了包括 mime 在内的每个信息。

    【讨论】:

      【解决方案4】:

      您可以使用在 android 中定义但声明为内部的 http 多部分类,而不是包含 jar 文件,因此您不能直接使用它们,您必须从 android source code 下载它们并将它们添加到您的项目中。使用前必须进行一些基本修改,没什么困难(更改包名称,删除所有记录器 来电)。它非常适合我。我希望这可以帮助某人,

      【讨论】:

        【解决方案5】:

        因此,尽管上述所有答案确实有助于正确解决问题。

        但仅删除 HttpMultipartMode.BROWSER_COMPATIBLE 并不能解决问题。

        您可以通过这样做来设置mime类型

        ByteArrayBody bab = new ByteArrayBody(params.getByteArray("data"), "image/jpeg" , "image.jpg");
        

        注意我正在使用最新的重新打包的android客户端http://code.google.com/p/httpclientandroidlib/,它支持分段上传等。

        【讨论】:

          【解决方案6】:

          我们这里有同样的问题。使用HttpMultipartMode.BROWSER_COMPATIBLE 删除了发送的正确mimetype。

          不幸的是,使用“普通”MultipartEntity() 对我们来说不是一个选项,因为这样 Rails 后端会扰乱请求处理。

          有没有办法自己设置多部分的内容类型并使用HttpMultipartMode.BROWSER_COMPATIBLE??

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-05-12
            • 1970-01-01
            • 2013-08-08
            • 2011-05-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多