编程语言
首页 > 编程语言> > java – Android:上传文件,同时填写POST正文

java – Android:上传文件,同时填写POST正文

作者:互联网

我确实使用MultipartEntity将File发送到服务器,它在$_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,documentation的一部分

与此类似:Android: Uploading a file to a page along with other POST strings

解决方法:

只需在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
)

这并不会压缩一个内容正文部分内的所有后变量,但它会对作业进行处理.

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

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”.

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

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”.

我最好的猜测是将所有数据添加为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&Send=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']);

标签:android,java,php,file-upload,multipartentity
来源: https://codeday.me/bug/20190927/1823410.html