安卓:上传文件与填写POST正文一起



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

但我也需要填写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的一部分,文档

类似于此:Android:将文件与其他POST字符串一起上传到页面

只需在MultipartEntity中添加几个FormBodyPart

您可以使用StringBody对象来提供该值。

以下是如何使用它的示例:

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脚本的输出:

$_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
)

这不会压缩一个内容主体部分内的所有post-var,但它可以完成任务。

您无法从访问数据php://stdin或$HTTP_RAW_POST_DATA,两者都不可用于多部分/表单数据编码。来自PHP文档:

php://input是一个只读流,允许您读取原始数据来自请求主体。在POST请求的情况下,最好使用php://input而不是$HTTP_RAW_POST_DATA,因为它不是取决于特殊的php.ini指令。此外,对于那些默认情况下不会填充$HTTP_RAW_POST_DATA,它可能是较少占用内存的激活替代方案always_popuple_raw_post_data。php://input不适用于enctype="多部分/表单数据"。

即使您将always_populate_raw_post_data设置为On,它仍然无法解决问题:

始终填充包含原始POST数据的$HTTP_RAW_POST_DATA。否则,变量只填充无法识别的MIME类型的数据。但是,访问原始POST的首选方法数据是php://input.$HTTP_RAW_POST_DATA不可用于enctype="多部分/表单数据"。

我的最佳猜测是将所有数据添加为ByteArrayBodyStringBody把它当作你在读php://stdin

以下是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']));

你应该得到:

string(21)"b=a&c=a&d=a&aamp;Send=发送"

编辑:经过深思熟虑,我认为最好只使用一个StringBody,将所有数据放在一个post变量中,然后从中解析它,它跳过将数据写入文件并在请求后删除它,因为临时文件完全没有用,这将提高性能。这里有一个例子:

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

然后从PHP:

var_dump($_POST['rawData']);

最新更新