安卓:上传文件填写POST体一起 [英] Android: upload file with filling out POST body together

查看:229
本文介绍了安卓:上传文件填写POST体一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MultipartEntity将文件发送到服务器时,它正确地出现在 $ _ FILES 超全局

I do use MultipartEntity to send File to server, it appears correctly in $_FILES superglobal

不过,我也需要填写POST主体通过读PHP://标准输入

But I need also fill in POST body to be read via 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,<一部分href="http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html"相对=nofollow>文档

与此类似:安卓:与其他POST串上传文件到页面

推荐答案

只需添加AA几个<一href="http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/FormBodyPart.html"><$c$c>FormBodyPart你的<一个href="http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html"><$c$c>MultipartEntity.

Just add a a few FormBodyPart to your MultipartEntity.

您可以使用<一个href="http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/StringBody.html"><$c$c>StringBody对象提供的值

You can use the StringBody object to provide the value.

下面是如何使用它的一个例子:

Here is an example of how you can use it:

byte[] data = {10,10,10,10,10}; 
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("server url");
ByteArrayBody bab = new ByteArrayBody(data, "image.jpg");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("image", bab);

FormBodyPart bodyPart=new FormBodyPart("formVariableName", new StringBody("formValiableValue"));
reqEntity.addPart(bodyPart);
bodyPart=new FormBodyPart("formVariableName2", new StringBody("formValiableValue2"));
reqEntity.addPart(bodyPart);
bodyPart=new FormBodyPart("formVariableName3", new StringBody("formValiableValue3"));
reqEntity.addPart(bodyPart); 
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = null;
while((line = in.readLine()) != null) {
    System.out.println(line);
}

下面是PHP脚本的输出:

Here is the output of the PHP script:

$_FILES
Array
(
    [image] => Array
        (
            [name] => image.jpg
            [type] => application/octet-stream
            [tmp_name] => /tmp/php6UHywL
            [error] => 0
            [size] => 5
        )

)
$_POST:
Array
(
    [formVariableName] => formValiableValue
    [formVariableName2] => formValiableValue2
    [formVariableName3] => formValiableValue3
)

这不COM preSS一个内容主体部分里的所有POST变量,但剂量的工作。

This doesn't compress all the post vars inside of one content body part but it dose the job.

您不能从 PHP访问数据://标准输入或<一href="http://www.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data">$HTTP_RAW_POST_DATA,无论是多部分/ form-data编码不可用。从PHP文档:

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:

PHP://输入是一个只读的数据流,使您可以读取原始数据   请求主体。在POST请求的情况下,preferable   使用PHP://输入,而不是$ HTTP_RAW_POST_DATA,因为它不   依靠特殊的php.ini设置。此外,对于那些情况下   不填充默认情况下$ HTTP_RAW_POST_DATA,这是一个潜在的   更少的内存密集型的替代激活   always_populate_raw_post_data。 PHP://输入不可用   ENCTYPE =多部分/表单数据。

php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data".

即使您设置<一个href="http://www.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data"><$c$c>always_populate_raw_post_data要在它仍然不会解决问题:

Even if you set always_populate_raw_post_data to On it still won't fix the problem:

总是产生$ HTTP_RAW_POST_DATA变量包含有原始的POST数据。   否则,此变量仅在碰到未识别MIME类型   的数据。然而,preferred方法访问原始POST   数据是php://输入。 $ HTTP_RAW_POST_DATA不可用   ENCTYPE =多部分/表单数据。

Always populate the $HTTP_RAW_POST_DATA containing the raw POST data. Otherwise, the variable is populated only with unrecognized MIME type of the data. However, the preferred method for accessing the raw POST data is php://input. $HTTP_RAW_POST_DATA is not available with enctype="multipart/form-data".

我最好的猜测是刚刚添加的所有数据作为<一个href="http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/ByteArrayBody.html"><$c$c>ByteArrayBody或<一href="http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/StringBody.html"><$c$c>StringBody和公正 使用,如果你是从PHP读物://标准输入

My best guess is just add all the data as a ByteArrayBody or StringBody and just use that as if you were reading from php://stdin

下面是<一个例子href="http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/ByteArrayBody.html"><$c$c>ByteArrayBody:

String testString="b=a&c=a&d=a&Send=Send";
reqEntity.addPart(new FormBodyPart("formVariables", new ByteArrayBody(testString.getBytes(), "application/x-www-form-urlencoded", "formVariables")));

和在PHP中:

var_dump(file_get_contents($_FILES['formVariables']['tmp_name']));

您应该:​​

串(21)B = A和C = A和D = A和发送=发送

string(21) "b=a&c=a&d=a&Send=Send"

编辑:在一些新的想法,我认为这是最好只使用一个<一个href="http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/StringBody.html"><$c$c>StringBody并把所有的数据保存在一个岗位变量,然后从该分析它,它会跳过将数据写入一个文件,并请求之后将其删除,因为该临时文件是完全没用的,这将提高性能。 下面是一个例子:

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:

String testString="b=a&c=a&d=a&Send=Send";
bodyPart=new FormBodyPart("rawData", new StringBody(testString));
reqEntity.addPart(bodyPart);

再从PHP的:

Then from PHP:

var_dump($_POST['rawData']);

这篇关于安卓:上传文件填写POST体一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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