通过java httpclient模拟表单上传 PHP/5.3.3 上 empty($_FILES) 为空,通过html 代码直接表单提交就是正常的。 PHP/5.4.40 也是可以的
httpclient4.5.2 模拟请求的代码:
InputStreamBody streamBody = new InputStreamBody(inputStream, contentType, fileData.getFilename());
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.addPart("file", streamBody);
for (String key : params.keySet()) {
multipartEntityBuilder.addPart(key, new StringBody((String) params.get(key), ContentType.TEXT_PLAIN));
}
HttpEntity entity = multipartEntityBuilder
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.setContentType(MULTIPART_FORM_DATA_UTF8)
.setCharset(Consts.UTF_8).build();
httpPost.setEntity(entity);
HttpResponse response = HttpClientBuilder.create().build().execute(httpPost);
String resultStr = IOUtils.toString(response.getEntity().getContent());
php中接收的:
if(!empty($_FILES)){
}
这里判断的的时候为空。
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…