Android Multipart HTTP Post 不发送文件的 MIME 类型

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

试图弄清楚我的编码有什么问题。我关注了来自 here 的博客文章.

我设法获得了将文件实际上传到 PHP 网络服务的代码。然而,出于某种原因,虽然我已经为文件明确设置了 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;
}

非常感谢:)

最佳答案

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

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

当我改变这一行时:

MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

MultipartEntity mp = new MultipartEntity();

然后 mime 类型设置正确,服务上传成功。

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

关于Android Multipart HTTP Post 不发送文件的 MIME 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3482674/

相关文章:

asp.net-core - 单例 HttpClient 是否在 X 分钟后接收到新的 HttpMessageHandler

c# - 无法使用 C# HttpClient 上传文件,Postman 工作正常

tomcat - HttpServletResponse 如何获取 header 以检查大小

javascript - 通过ajax post请求发送默认数据

android - Retrofit post请求,url已有参数,body有请求参数,如何定义请求接口(interface)使用?

Android:如何在 Android 5.0+ 至 6.0 上阻止(或断开连接)调用

java - HttpClient 响应处理程序总是返回关闭的流

android - HttpPost 请求响应

android - 如何在 Google Play 等标签页中启用水平滚动?

android - 需要帮助改进 OpenCV 2.4.0 中人脸检测的执行时间