我试图让我的Android多部分图片上传工作使用默认的blueimp PHP UploadHandler,我将其用于我的应用的网络版。
它没有开箱即用,所以我尝试了请求的标题
我玩了很长一段时间,但由于没有任何效果,我最终启动了Wireshark并比较了blueimp演示的数据包和Android应用程序发送的数据包。
经过一些进一步的调整,他们看起来完全相同(以及重要部分):
Blueimp演示:http://i.imgur.com/T9styyR.png
Android MultipartEntity:http://i.imgur.com/OuDuyJ0.png
这是我的自定义generate_response
函数返回的内容:
Array
(
[0] => stdClass Object
(
[name] => 1387215600-9207
[size] => 97894
[type] => multipart/form-data; boundary=-----Don'tmindmeI'mjustaboundary
[error] => Filetype not allowed
)
)
似乎加载整个数据包而不仅仅是它的多部分
有没有办法解决这个问题?
以下是我创建请求的方式(不完全,但这些是相关的行):
public static String BOUNDARY = "-----Don'tmindmeI'mjustaboundary";
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, BOUNDARY, null);
File file = new File(params[2]); //params[2] contains the absolute path + filename
String filename = file.getName();
entity.addPart(filename, new FileBody(file, getMimeType(params[2]))); //getMimeType returns the MIME Type of the image e.g. image/png
HttpPost request = new HttpPost(params[0]); //params[0] contains the URL
request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
request.setEntity(entity);
HttpResponse response = client.execute(request, context);
我已经在这个工作了好几个小时了,这让我疯了。
我做错了什么?
修改:
没关系我让它工作了
我忘了将表单的名称设置为files [],这是UploadHandler正在寻找的...
所以现在它看起来像这样:
...
public static String BOUNDARY = "-----Don'tmindmeI'mjustaboundary";
...
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, BOUNDARY, null);
File file = new File(params[2]);
String filename = file.getName();
FileBody body = new FileBody(file, getMimeType(params[2]));
FormBodyPart part = new FormBodyPart("files[]", body);
part.addField("Content-Type", getMimeType(params[2]));
part.addField("Content-Disposition", "form-data; filename=\""+filename+"\"");
entity.addPart(part);
HttpPost request = new HttpPost(params[0]);
request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
request.addHeader("Accept", "application/json, text/javascript, */*; q=0.01");
request.addHeader("Accept-Language", "en-US,en;q=0.8,de;q=0.6,es;q=0.4");
request.setEntity(entity);
HttpResponse response = client.execute(request, context);
...