Android:上传文件并一起填写POST正文
我确实使用 MultipartEntity 将文件发送到服务器,它在 $_FILES
superglobal 中正确显示
但我还需要填写 POST 正文以通过 php://stdin
读取
如何我这样做?
当前代码片段如下:
ByteArrayOutputStream bos = new ByteArrayOutputStream(); // stream to hold image
bm.compress(CompressFormat.JPEG, 75, bos); //compress image
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("REMOTE ADDRESS");
ByteArrayBody bab = new ByteArrayBody(data, "image.jpg");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE); // is this one causing trouble?
reqEntity.addPart("image", bab); // added image to request
// tried this with no luck
// reqEntity.addPart("", new StringBody("RAW DATA HERE"));
postRequest.setEntity(reqEntity); // set the multipart entity to http post request
HttpResponse response = httpClient.execute(postRequest);
MultipartEntity 是 HttpMime 4.1.2 API 的一部分,文档
I do use MultipartEntity to send File to server, it appears correctly in $_FILES
superglobal
But I need also fill in POST body to be read via php://stdin
How can I do that?
current snippet below:
ByteArrayOutputStream bos = new ByteArrayOutputStream(); // stream to hold image
bm.compress(CompressFormat.JPEG, 75, bos); //compress image
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("REMOTE ADDRESS");
ByteArrayBody bab = new ByteArrayBody(data, "image.jpg");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE); // is this one causing trouble?
reqEntity.addPart("image", bab); // added image to request
// tried this with no luck
// reqEntity.addPart("", new StringBody("RAW DATA HERE"));
postRequest.setEntity(reqEntity); // set the multipart entity to http post request
HttpResponse response = httpClient.execute(postRequest);
MultipartEntity is part of HttpMime 4.1.2 API, documentation
Similar to this: Android: Uploading a file to a page along with other POST strings
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需添加一些
FormBodyPart
到您的
MultipartEntity
。您可以使用
StringBody
对象提供值。以下是如何使用它的示例:
这是 PHP 脚本的输出:
这不会压缩一个内容主体部分内的所有帖子变量,但它完成了这项工作。
您无法从 php://stdin 或 $HTTP_RAW_POST_DATA,两者均不适用于 multipart/form-data 编码。来自 PHP 文档:
即使您设置
always_populate_raw_post_data
到 On 仍然无法解决问题:我最好的猜测是将所有数据添加为
ByteArrayBody
或StringBody
只是使用它就像从 php://stdin 中阅读一样
这是
ByteArrayBody
:在 PHP 中:
您应该得到:
编辑:经过一番思考,我认为最好只使用一个
StringBody
并将所有数据放入一个 post 变量中,然后从那里解析它,它跳过将数据写入文件并在请求后删除它,因为临时文件完全无用,这将提高性能。这是一个例子:
然后从 PHP:
Just add a a few
FormBodyPart
to yourMultipartEntity
.You can use the
StringBody
object to provide the value.Here is an example of how you can use it:
Here is the output of the PHP script:
This doesn't compress all the post vars inside of one content body part but it dose the job.
You can't access the data from php://stdin or $HTTP_RAW_POST_DATA, both are unavailable for multipart/form-data encoding. From the PHP docs:
Even if you set
always_populate_raw_post_data
to On it still won't fix the problem:My best guess is just add all the data as a
ByteArrayBody
orStringBody
and justuse that as if you were reading from php://stdin
Here is an example of
ByteArrayBody
:And in PHP:
You should get:
Edit: After some second thoughts I think it's better to just use one
StringBody
and put all the data in one post variable, then parse it from that, it skips writing the data to a file and deleting it after the request since the temp file is totally useless this will increase performance.Here is an example:
Then from PHP: