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

发布于 2024-09-14 00:17:07 字数 2308 浏览 13 评论 0原文

试图找出我的编码有什么问题。我关注了此处的博客文章。

我设法获得了将文件实际上传到 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;
}

非常感谢之前:)

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

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.

Here's my codings:

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

别靠近我心 2024-09-21 00:17:07

我遇到了同样的问题,刚刚修复了它。

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

当我更改此行时:

MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

然后

MultipartEntity mp = new MultipartEntity();

mime 类型设置正确并且服务上传工作。

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

I had the same problem and just fixed it.

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.

When I changed this line:

MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

to

MultipartEntity mp = new MultipartEntity();

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

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

哆兒滾 2024-09-21 00:17:07

我刚刚将 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时,将检查要格式化的部分的filenameBROWSER_COMPATIBLE中是否不为空模式。如果为 null,则忽略整个主体。

I just had the same issue when updating HttpClient from version 4.1 to version 4.4. Neither removing HttpMultipartMode.BROWSER_COMPATIBLE
nor replacing with other alternative options did help.

A solution that requires the minimum change for me is to explicitly specify the filename when constructing a FileBody, i.e. replacing

new FileBody(file, mimeType)

with

new FileBody(file, filename, mimeType, charset)

As for version HttpClient 4.4, the reason might be that new FileBody(file, mimeType) assigns null to the filename field rather than uses file.getName(). Then, when executing formatMultipartHeader in HttpMultipart, the filename of the part that is going to be formatted is checked if not null in BROWSER_COMPATIBLE mode. If null, then the whole body is ignored.

欲拥i 2024-09-21 00:17:07

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

I had the same problem for awhile for posting 3 FileBodies and 1 mime info, the mime (in json) was never received unless I sent only one filebody. I always addpart the filebody first which caused me the problem. I reversed the order by addpart the mime at very beginning and every info including mime are received correctly at php server end.

游魂 2024-09-21 00:17:07

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

Instead of including the jars files you can use the http multipart classes defined in android but declared as internal, so you cannot use them directly, you have to download them from the android source code and add them to your project. Some basics modifications must be performed before using them, nothing difficult (change the packages name, remove all logger
calls). It works perfectly for me.I hope this could help somebody,

宫墨修音 2024-09-21 00:17:07

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

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

您可以通过执行此操作来设置 mime 类型

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

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

So while all the above answers did help in getting it right.

But just removing the HttpMultipartMode.BROWSER_COMPATIBLE wouldn't solve it.

You can set the mime type by doing this

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

N.B. I am using the latest repackaged android client http://code.google.com/p/httpclientandroidlib/ which supports multipart uploads, etc.

故事未完 2024-09-21 00:17:07

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

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

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

We have the same issue here. Using HttpMultipartMode.BROWSER_COMPATIBLE removed the correct mimetype being sent.

Unfortunately using the 'normal' MultipartEntity() is not an option for us, as this way the rails backend messes up the request handling.

Is there any way to set the content-type of the multipart yourself and use HttpMultipartMode.BROWSER_COMPATIBLE??

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文