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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐