Android Multipart HTTP Post 不发送文件的 MIME 类型
试图找出我的编码有什么问题。我关注了此处的博客文章。
我设法获得了将文件实际上传到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我遇到了同样的问题,刚刚修复了它。
我发现,当对图像使用
ByteArrayBody
时,使用HttpMultipartMode.BROWSER_COMPATIBLE
会阻止在我的请求中设置正确的 mimeType。我认为这可能与您遇到的问题相同。当我更改此行时:
然后
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 aByteArrayBody
for an image. I assume this is probably the same problem you are having.When I changed this line:
to
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.我刚刚将 HttpClient 从版本 4.1 更新到版本 4.4 时遇到了同样的问题。既不删除
HttpMultipartMode.BROWSER_COMPATIBLE
用其他替代选项替换也没有帮助。
对我来说需要最少更改的解决方案是在构造
FileBody
时显式指定filename
,即替换为
对于 HttpClient 4.4 版本,原因可能是
new FileBody(file, mimeType)
将null
分配给filename
字段,而不是使用file.getName()
。然后,当在HttpMultipart
中执行formatMultipartHeader
时,将检查要格式化的部分的filename
在BROWSER_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 aFileBody
, i.e. replacingwith
As for version HttpClient 4.4, the reason might be that
new FileBody(file, mimeType)
assignsnull
to thefilename
field rather than usesfile.getName()
. Then, when executingformatMultipartHeader
inHttpMultipart
, thefilename
of the part that is going to be formatted is checked if not null inBROWSER_COMPATIBLE
mode. If null, then the whole body is ignored.我在发布 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.
您可以使用 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,
因此,尽管上述所有答案确实有助于正确解决问题。
但仅删除 HttpMultipartMode.BROWSER_COMPATIBLE 并不能解决问题。
您可以通过执行此操作来设置 mime 类型
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
N.B. I am using the latest repackaged android client http://code.google.com/p/httpclientandroidlib/ which supports multipart uploads, etc.
我们这里有同样的问题。使用 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
??