通过 Java httpclient 模拟表单上传 PHP /5.3.3 上 empty($_FILES) 为空,通过 html 代码直接表单提交就是正常的。 PHP /5.4.40 也是可以的

2017-04-01 11:34:04 +08:00
 zorui

通过 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)){
    } 

这里判断的的时候为空。

2989 次点击
所在节点    Java
12 条回复
junbaor
2017-04-01 11:42:31 +08:00
zorui
2017-04-01 11:42:55 +08:00
现在确定的就是 php 中 php.ini 配置 post_max_size 是大于要上传的文件的。
zorui
2017-04-01 12:04:24 +08:00
@junbaor jar 包中目前还不能换。
hcymk2
2017-04-01 12:36:51 +08:00
抓个包和与表单提交的对比下先.
ijustdo
2017-04-01 12:46:52 +08:00
呵呵 不要查肯定是 post 文件的时候 Content-Type 不对的呢 ^_^
zorui
2017-04-01 13:21:32 +08:00
Content-Type 都是 multipart/form-data
zorui
2017-04-01 13:28:17 +08:00
zorui
2017-04-01 13:33:07 +08:00
8355
2017-04-01 15:27:47 +08:00
php://input 接收二进制流应该可以的.
试试吧
hcymk2
2017-04-01 16:21:13 +08:00
zorui
2017-04-06 15:14:00 +08:00
@hcymk2
@8355
@ijustdo
@zorui 找到原因了。通过 httpclient 出去的 header 中是使用的是 Transfer-Encoding: chunked , php5.3 不支持这个,只支持 Content-Length. 表单提交也是通过 Content-Length 所以就可以。 php5.4 是支持的。
hcymk2
2017-04-06 15:17:35 +08:00
@zorui
你看了那 2 个连接里面的内容应该就知道了吧....

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/351945

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX