Android的多部分HTTP POST不发送文件的MIME类型 [英] Android Multipart HTTP Post does not send the File's MIME type

查看:120
本文介绍了Android的多部分HTTP POST不发送文件的MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图弄清楚什么是错我的编码。我跟着从这里 博客文章。

Trying to figure what's wrong with my codings. I followed a blog post from here.

我设法得到codeS实际的文件上传到PHP的Web服务。然而,由于某种原因,虽然我已经明确设置的MIME类型的文件,PHP表明MIME是只是一个空字符串,因此拒绝。

I managed to get the codes to actually upload the file to a PHP web service. However, for some reason although I've set explicitly the MIME type for the file, PHP shows that the MIME is just a blank string and therefore rejected.

下面是我的编码:

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;
}

非常感谢前:)

Many thanks before :)

推荐答案

我有同样的问题,只是固定它。

I had the same problem and just fixed it.

我发现,使用 HttpMultipartMode.BROWSER_COMPATIBLE $ P $从我的要求下被设置pvented正确MIMETYPE,当使用 ByteArrayBody 的图像。我想这可能是你有同样的问题。

I found that using the HttpMultipartMode.BROWSER_COMPATIBLE prevented the correct mimeType from being set in my request, when using a ByteArrayBody for an image. I assume this is probably the same problem you are having.

当我改变了这一行:

MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

MultipartEntity mp = new MultipartEntity();

那么MIME类型设置正确和服务的上传工作。

then the mime type was set correctly and the service upload worked.

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

I see that people used the BROWSER_COMPATIBLE for solving another problem, but hopefully you don't need it.

这篇关于Android的多部分HTTP POST不发送文件的MIME类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆