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

Tis*_*sta 10 android multipartform-data httpclient http-post mime-types

试图弄清楚我的编码有什么问题.我从这里关注了一篇博文.

我设法获得代码实际上传文件到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;
}
Run Code Online (Sandbox Code Playgroud)

非常感谢之前:)

小智 5

我有同样的问题,只是修复它.

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

当我更改此行时:

MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
Run Code Online (Sandbox Code Playgroud)

MultipartEntity mp = new MultipartEntity();
Run Code Online (Sandbox Code Playgroud)

然后正确设置了mime类型并且服务上传工作.

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