1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost post = new HttpPost(url); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setBoundary("123456789"); builder.setCharset(java.nio.charset.Charset.forName("UTF-8")); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); // 文件流 builder.addBinaryBody("file", getInputStream(), ContentType.DEFAULT_BINARY, "xyz.txt"); builder.addPart("type", new StringBody("0", ContentType.TEXT_PLAIN)); //Finally post.setEntity(builder.build()); CloseableHttpResponse response = httpClient.execute(post); response.getEntity().writeTo(System.out); |
POST /ebotApi/aduioInfo/add HTTP/1.1[\r][\n]
Transfer-Encoding: chunked[\r][\n]
Content-Type: multipart/form-data; boundary=123456789; charset=UTF-8[\r][\n]
Host: localhost:8080[\r][\n]
Connection: Keep-Alive[\r][\n]
User-Agent: Apache-HttpClient/4.5.13 (Java/17.0.4)[\r][\n]
Accept-Encoding: gzip,deflate[\r][\n]
[\r][\n]
9a[\r][\n]
--123456789[\r][\n]
Content-Disposition: form-data; name="file"; filename="xyz.txt"[\r][\n]
Content-Type: application/octet-stream[\r][\n]
[\r][\n]
hello javacoder.cn[\r][\n]
3f[\r][\n]
[\r][\n]
--123456789[\r][\n]
Content-Disposition: form-data; name="type"[\r][\n]
[\r][\n]
0[\r][\n]
11[\r][\n]
[\r][\n]
--123456789--[\r][\n]
[\r][\n]
0[\r][\n]
[\r][\n]
content-type:multipart/form-data
Comments are closed.