android上传文件from-data,java – Android:上传文件,同时填写POST正文

这篇博客介绍了如何使用HttpClient库在Java中构造一个MultipartEntity,包含多个FormBodyPart,将数据发送到PHP服务器。示例代码展示了如何发送byte数组(例如图片数据)和字符串变量,并解释了在PHP端如何访问这些数据。博客还讨论了$HTTP_RAW_POST_DATA在多部分表单数据编码中不可用的问题,建议直接通过$_POST变量获取数据以提高性能。
摘要由CSDN通过智能技术生成

只需在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 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”.

我最好的猜测是将所有数据添加为ByteArrayBody或StringBody

使用它就像你从php:// stdin中读取一样

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值