一个做的方式是通过独特的分离器($)
Content-Disposition: form-data; name="uploaded_file";filename=""+ fileName +"_$_stringdata"+ """ + lineEnd
在PHP中分离内容处置一起发送字符串,http://php.net/manual/en/httpresponse.getcontentdisposition.php
使用getContentDisposition并解析它。
Description
static string HttpResponse::getContentDisposition (void)
Get current Content-Disposition setting.
其他方式:使用httpmime 只需添加一个几FormBodyPart您MultipartEntity。
您可以使用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
)
关于以同样的方式像你一样与文件:在两个边界之间。您的文件内容只是多部分数据的一部分。您可以根据需要添加任意数量的部件。零件按边界分开。 – greenapps
@greenapps你能告诉我怎么做?我需要为字符串添加setRequestProperty()吗?我需要Content-Disposition吗? – user5155835