Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Apache HttpClient上传后文件赢得';无法使用MultipartEntityBuilder上载_Java_Apache_Apache Httpclient 4.x - Fatal编程技术网

Java Apache HttpClient上传后文件赢得';无法使用MultipartEntityBuilder上载

Java Apache HttpClient上传后文件赢得';无法使用MultipartEntityBuilder上载,java,apache,apache-httpclient-4.x,Java,Apache,Apache Httpclient 4.x,我有一个HTML表单,里面有一个文件上传。我正试图使用MultipartEntityBuilder通过表单上传Word文档,但它不起作用 表单中有几个文本输入和两行文件上传行。如果我使用BasicNameValuePair方法并将这些字段设置为空字符串,则文本字段将正确提交。由于我需要上传文件,我改为MultipartEntityBuilder,甚至无法让基本测试再次工作 工作代码 HttpClient client = HttpClientBuilder.create().build(); S

我有一个HTML表单,里面有一个文件上传。我正试图使用
MultipartEntityBuilder
通过表单上传Word文档,但它不起作用

表单中有几个文本输入和两行文件上传行。如果我使用
BasicNameValuePair
方法并将这些字段设置为空字符串,则文本字段将正确提交。由于我需要上传文件,我改为
MultipartEntityBuilder
,甚至无法让基本测试再次工作

工作代码

HttpClient client = HttpClientBuilder.create().build();
String uploadUrl = "http://somepage.com/upload";

HttpPost httppost = new HttpPost(uploadUrl);

httppost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
httppost.setHeader("Content-Type", "multipart/form-data");
httppost.setHeader("Accept-Encoding", "gzip, deflate");

List<NameValuePair> formparams = new ArrayList<NameValuePair>();

formparams.add(new BasicNameValuePair("testField1", "Value1"));
formparams.add(new BasicNameValuePair("testField2", "Value2"));
formparams.add(new BasicNameValuePair("fileField1", ""));
formparams.add(new BasicNameValuePair("fileField2", ""));

httppost.setEntity(new UrlEncodedFormEntity(formparams, Consts.UTF_8));

HttpResponse response = client.execute(httppost, httpContext);
该表单由Oracle托管并返回错误:

发生意外错误:ORA-06501:PL/SQL:程序错误

我还尝试了以下其他方法:

builder.addTextBody("textField1", "Value1");
builder.addTextBody("textField2", "Value2");

File file1 = new File("path to file1");
builder.addBinaryBody("fileField1", file1, ContentType.DEFAULT_BINARY, file1.getName());

File file2 = new File("path to file2");
builder.addBinaryBody("fileField2", file2, ContentType.DEFAULT_BINARY, file2.getName());
以及尝试模拟我的第一个测试,将所有内容放入
builder.addTextBody()
中,就像我最初的工作测试一样,但仍然得到相同的结果

日志记录

根据@oleg请求,我已发布了此次通话的通话记录:

DEBUG [org.apache.http.client.protocol.RequestAddCookies] CookieSpec selected: default
DEBUG [org.apache.http.client.protocol.RequestAddCookies] Cookie [version: 0][name: SSO_TIMEOUT_ID][value: v1.0~83BA4EF3DA76C07B55F93B5C5D65F90947314693035F046BFCC21BCD37F8B95284E732E711971532B182F90AE461E320FCCC74452BAF4A16FB6E5EFA5F86985B26C95D30001D9ACC3BE8E9D2786B1CD38A79788FC7623FCE06C84266C234638182D44786E4971B53EBFC25FD3B7A565F][domain: isomething.com][path: /][expiry: null] match [wwwdev.isomething.com:80/portal/pls/cust_portal/!CUST_PORTAL.wwa_app_module.accept]
DEBUG [org.apache.http.client.protocol.RequestAddCookies] Cookie [version: 0][name: cust_portal][value: 9.0.3+en-us+us+AMERICA+12955AE0CEA20F3CE050558C15F00BD2+84676D40A18761D45DEBA039A78FF868CA9B49F2DEA2D283DE61561CE0F547D3A27C643219F1E6C867CF150CDEA69AE9844407F570B4BBD967491098CECEEA836861C9FF1F06AF7929814DF3C55426F1C2E73C91B219801B][domain: wwwdev.isomething.com][path: /][expiry: null] match [wwwdev.isomething.com:80/portal/pls/cust_portal/!CUST_PORTAL.wwa_app_module.accept]
DEBUG [org.apache.http.client.protocol.RequestAuthCache] Auth cache not set in the context
DEBUG [org.apache.http.impl.conn.PoolingHttpClientConnectionManager] Connection request: [route: {}->http://wwwdev.isomething.com:80][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
DEBUG [org.apache.http.impl.conn.PoolingHttpClientConnectionManager] Connection leased: [id: 4][route: {}->http://wwwdev.isomething.com:80][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
DEBUG [org.apache.http.impl.execchain.MainClientExec] Opening connection {}->http://wwwdev.isomething.com:80
DEBUG [org.apache.http.impl.conn.DefaultHttpClientConnectionOperator] Connecting to wwwdev.isomething.com/141.146.161.39:80
DEBUG [org.apache.http.impl.conn.DefaultHttpClientConnectionOperator] Connection established 10.0.0.2:49827<->141.146.161.39:80
DEBUG [org.apache.http.impl.execchain.MainClientExec] Executing request POST /portal/pls/cust_portal/!CUST_PORTAL.wwa_app_module.accept HTTP/1.1
DEBUG [org.apache.http.impl.execchain.MainClientExec] Target auth state: UNCHALLENGED
DEBUG [org.apache.http.impl.execchain.MainClientExec] Proxy auth state: UNCHALLENGED
DEBUG [org.apache.http.headers] http-outgoing-4 >> POST /portal/pls/cust_portal/!CUST_PORTAL.wwa_app_module.accept HTTP/1.1
DEBUG [org.apache.http.headers] http-outgoing-4 >> Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
DEBUG [org.apache.http.headers] http-outgoing-4 >> Origin: http://wwwdev.isomething.com
DEBUG [org.apache.http.headers] http-outgoing-4 >> User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36
DEBUG [org.apache.http.headers] http-outgoing-4 >> Referer: http://wwwdev.isomething.com/portal/page/cust_portal/SOM_PGR/ReportManager2/ReportManager/SOM_Reports_MTC/SOM_FILE_CLERK_ADD_REPORT?np_report_number=MTC01234567&np_project_number=3141307&np_country=US&np_customer_number=110960&np_module=URM&np_customer_name=MY TEST CENTRAL TEST ACCOUNT
DEBUG [org.apache.http.headers] http-outgoing-4 >> Accept-Encoding: gzip, deflate
DEBUG [org.apache.http.headers] http-outgoing-4 >> Accept-Language: en-US,en;q=0.8
DEBUG [org.apache.http.headers] http-outgoing-4 >> DNT: 1
DEBUG [org.apache.http.headers] http-outgoing-4 >> Content-Length: 49234
DEBUG [org.apache.http.headers] http-outgoing-4 >> Content-Type: multipart/form-data; boundary=_I6HZ69Fquh0RHe-DNWGs9tfX48pemmkOYs
DEBUG [org.apache.http.headers] http-outgoing-4 >> Host: wwwdev.isomething.com
DEBUG [org.apache.http.headers] http-outgoing-4 >> Connection: Keep-Alive
DEBUG [org.apache.http.headers] http-outgoing-4 >> Cookie: SSO_TIMEOUT_ID=v1.0~83BA4EF3DA76C07B55F93B5C5D65F90947314693035F046BFCC21BCD37F8B95284E732E711971532B182F90AE461E320FCCC74452BAF4A16FB6E5EFA5F86985B26C95D30001D9ACC3BE8E9D2786B1CD38A79788FC7623FCE06C84266C234638182D44786E4971B53EBFC25FD3B7A565F; cust_portal=9.0.3+en-us+us+AMERICA+12955AE0CEA20F3CE050558C15F00BD2+84676D40A18761D45DEBA039A78FF868CA9B49F2DEA2D283DE61561CE0F547D3A27C643219F1E6C867CF150CDEA69AE9844407F570B4BBD967491098CECEEA836861C9FF1F06AF7929814DF3C55426F1C2E73C91B219801B
DEBUG [org.apache.http.headers] http-outgoing-4 << HTTP/1.1 200 OK
DEBUG [org.apache.http.headers] http-outgoing-4 << Cache-Control: max-age=0
DEBUG [org.apache.http.headers] http-outgoing-4 << Content-Type: text/html; charset=UTF-8
DEBUG [org.apache.http.headers] http-outgoing-4 << Set-Cookie: SSO_TIMEOUT_ID=v1.0~83BA4EF3DA76C07B68FCC2530F599A0EC0C64F824C7AE3F72C95A8D07625F4915248DB9B7D40E28DF645BB373ADAE5E39B2A539F98F48507192E9993DAFEDE4D30331E7912A944E0A9C203BD851C0C0D7DCBC672186F9DF652220BC26B85C327A81DE6656E5D73550FCD2EA1BA53552F; domain=.isomething.com; path=/
DEBUG [org.apache.http.headers] http-outgoing-4 << Connection: Keep-Alive
DEBUG [org.apache.http.headers] http-outgoing-4 << Keep-Alive: timeout=5, max=999
DEBUG [org.apache.http.headers] http-outgoing-4 << Server: Oracle-Application-Server-10g/10.1.2.3.0 Oracle-HTTP-Server OracleAS-Web-Cache-10g/10.1.2.3.2 (N;ecid=100569954162,0)
DEBUG [org.apache.http.headers] http-outgoing-4 << Content-Length: 5134
DEBUG [org.apache.http.headers] http-outgoing-4 << Date: Tue, 31 Mar 2015 13:06:53 GMT
DEBUG [org.apache.http.headers] http-outgoing-4 << Content-Location: /servlet/RepositoryServlet/cust_portal/!CUST_PORTAL.wwa_app_module.accept
DEBUG [org.apache.http.impl.execchain.MainClientExec] Connection can be kept alive for 5000 MILLISECONDS
DEBUG [org.apache.http.client.protocol.ResponseProcessCookies] Cookie accepted [SSO_TIMEOUT_ID="v1.0~83BA4EF3DA76C07B68FCC2530F599A0EC0C64F824C7AE3F72C95A8D07625F4915248DB9B7D40E28DF645BB373ADAE5E...", version:0, domain:isomething.com, path:/, expiry:null]
DEBUG[org.apache.http.client.protocol.RequestAddCookies]CookieSpec selected:default
DEBUG[org.apache.http.client.protocol.RequestAddCookies]Cookie[version:0][name:SSO_TIMEOUT_ID][value:v1.0~83Ba4EF3DA76C07B55F93B5C5D65F9094731469035F046BFCC21BCD37F8B95284E732E711971532B182F90AE461E320FCCC74452BAF4A16FB6E5EFA56985B26C95D30001D9AC8E9E9E9E9D2786B1CD388FC7623FCE06C846381868E477EB537F477F457F457F477B867F457F457F457F457F477B7676767676767676767676[domain:isomething.com][path://][expiry:null]匹配[wwdev.isomething.com:80/portal/pls/cust_portal/!cust_portal.wwa_app_module.accept]
DEBUG[org.apache.http.client.protocol.RequestAddCookies]Cookie[version:0][name:cust_portal][value:9.0.3+en-us+us+AMERICA+12955AE0CEA20F3CE0558C15F00BD2+84676D40A18761D45DEA0398FF868CA9F2DEA283DE615CE0F547A27C643219F1E6C867CF150CDEA69AE9844407F570B4D967491098CEEEA836861C6C1FF1F798C554C29F20173918B][域名:wwdev.isomething.com][path://][expiry:null]匹配[wwdev.isomething.com:80/portal/pls/cust_portal/!cust_portal.wwa_app_module.accept]
调试[org.apache.http.client.protocol.RequestAuthCache]未在上下文中设置身份验证缓存
调试[org.apache.http.impl.conn.PoolighttpClientConnectionManager]连接请求:[路由:{}->http://wwwdev.isomething.com:80][保持活动状态的总数:0;分配的路由:0,共2条;分配的总数:0,共20条]
调试[org.apache.http.impl.conn.PoolighttpClientConnectionManager]租用的连接:[id:4][路由:{}->http://wwwdev.isomething.com:80][保持活动状态的总数:0;分配的路由:1/2;分配的总数:1/20]
调试[org.apache.http.impl.execchain.MainClientExec]打开连接{}->http://wwwdev.isomething.com:80
调试[org.apache.http.impl.conn.DefaultHttpClientConnectionOperator]连接到wwdev.isomething.com/141.146.161.39:80
调试[org.apache.http.impl.conn.DefaultHttpClientConnectionOperator]连接已建立10.0.0.2:49827141.146.161.39:80
调试[org.apache.http.impl.execchain.MainClientExec]执行请求POST/portal/pls/cust_portal/!cust_portal.wwa_app_module.accept http/1.1
调试[org.apache.http.impl.execchain.MainClientExec]目标身份验证状态:未被质询
调试[org.apache.http.impl.execchain.MainClientExec]代理身份验证状态:未被质询
调试[org.apache.http.headers]http-outgoing-4>>POST/portal/pls/cust_portal/!cust_portal.wwa_app_module.accept http/1.1
DEBUG[org.apache.http.headers]http-outing-4>>接受:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
调试[org.apache.http.headers]http-outing-4>>来源:http://wwwdev.isomething.com
DEBUG[org.apache.http.headers]http-outgoing-4>>用户代理:Mozilla/5.0(Macintosh;英特尔Mac OS X 10_10_2)AppleWebKit/537.36(KHTML,如Gecko)Chrome/41.0.2272.104 Safari/537.36
调试[org.apache.http.headers]http-outing-4>>参考:http://wwwdev.isomething.com/portal/page/cust_portal/SOM_PGR/ReportManager2/ReportManager/SOM_Reports_MTC/SOM_FILE_CLERK_ADD_REPORT?np_report_number=MTC01234567&np_project_number=3141307&np_country=US&np_customer_number=110960&np_module=URM&np_customer_name=MY 测试中心测试帐户
调试[org.apache.http.headers]http-outing-4>>接受编码:gzip,deflate
调试[org.apache.http.headers]http-outing-4>>接受语言:en-US,en;q=0.8
调试[org.apache.http.headers]http-outing-4>>DNT:1
调试[org.apache.http.headers]http-outing-4>>内容长度:49234
调试[org.apache.http.headers]http-outing-4>>内容类型:多部分/表单数据;边界=_I6HZ69Fquh0RHe-DNWGs9tfX48pemmkOYs
调试[org.apache.http.headers]http-outing-4>>主机:wwwdev.isomething.com
调试[org.apache.http.headers]http-outing-4>>连接:保持活动状态
调试[org.apache.http.headers]http-outgoing-4>>Cookie:SSO_TIMEOUT_ID=v1.0~83ba4ef3da76c07b55f93b5d65f9094731469035f046bfcc21bcd37f8b95284e732e711971532b182f90ae461e320fccc74452baf4a16fb6e5f8685b26c95d3001d9acc8f79788fc7623fco6c84266c23fc3463818f447886e971b532f25f357a5b65f;cust+castea555f088f085f50c25f50c258f50c25f258f25a5b+ce8f082b=美国门户网站0A18761D45DEBA039A78FF868CA9B49F2DEA283DE61561CE0F547D3A27C643219F1E6C867CF150CDEA69AE9844407F570B4BD967491098CEEEA836861C9FF1F06AF7929814DF3C55426F1C2E73C91B219801B

调试[org.apache.http.headers]http-outgoing-4不久前,我花了很多时间试图提交包含文件的多部分表单。最后,通过添加边界解决了问题。正如我在一些调试之后所理解的,没有边界,服务器无法识别部分

String boundary = "---------------"+UUID.randomUUID().toString();
multipartEntityBuilder.setBoundary(boundary);
...
HttpPost request = new HttpPost(uriBuilder.build());
request.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.getMimeType()+";boundary="+boundary);
...
multipartEntityBuilder.addBinaryBody("document[file]", dataBytes, ContentType.APPLICATION_OCTET_STREAM, name);
尝试删除此行

httppost.setHeader("Content-Type", "multipart/form-data");
已更新

正如预期的那样,应用程序正在发送无效的内容类型标头

[org.apache.http.headers] http-outgoing-5 >> Content-Type: multipart/form-data

请不要手动设置
内容类型
,让HttpClient根据请求中包含的
HttpEntity
的属性为您生成内容类型

我为此挣扎了……太久。几个小时了!无论如何,我没有
[org.apache.http.headers] http-outgoing-5 >> Content-Type: multipart/form-data
@Getter
public class MultipartMap {
    private static final String GOT_NUTHIN_TO_MULTIPART_POST = "Got nuthin to multipart-post!!";
    private final byte[] toPost;
    public final String DEFAULT_FILENAME = "unknownFile";
    private final String filename;
    private Map<String, String> textParams = new HashMap<>();

    public MultipartMap(byte[] data, String fileName, MultipartTextField... multipartTextField) {
        this.toPost = data;
        this.filename = StringUtils.hasContents(fileName) ? fileName : DEFAULT_FILENAME;
        Arrays.stream(multipartTextField).forEach(param -> textParams.put(param.getFormName(), param.getFormValue()));
    }

    public void isSane() {
        if (toPost == null || toPost.length == 0) {
            throw new WrappedHttpClientException(GOT_NUTHIN_TO_MULTIPART_POST);
        }
    }
}
    private static final String FILE = "file";
    private static final String CONTENT_TYPE = "content-type";
    ..
    multipartMap.isSane();
    isMultiPartRequest = true;
    
    final MultipartEntityBuilder multipartBuilderInner = MultipartEntityBuilder.create();
    multipartBuilderInner.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    
    multipartBuilderInner.addBinaryBody(
        FILE,
        multipartMap.getToPost(),
        ContentType.DEFAULT_BINARY,
        multipartMap.getFilename()
    );
    
    multipartMap.getTextParams().entrySet().stream().forEach(e -> {
        multipartBuilderInner.addTextBody(e.getKey(), e.getValue(), ContentType.TEXT_PLAIN);
    });
    ..
    HttpPost request = new HttpPost(url);   
    request.setEntity(multipartBuilderInner.build());
    
    ..          
    if (isMultiPartRequest && req.getAllHeaders() != null) {
        request.removeHeaders(CONTENT_TYPE); // this is IMPORTANT for multipart-requests!!
    }
    @PostMapping(path = "saveDocument", produces = "application/json; charset=utf-8", consumes = "multipart/form-data")
    public ResponseEntity<String> saveDocument(@RequestPart(value = "file", required = false) MultipartFile file, @RequestPart(value = "filename", required = false) String filename,..