在本教程中,我们将演示如何使用Apache HttpClient 4.5进行分段上传http请求。
Maven依赖关系
我们使用maven来管理依赖关系,并使用Apache HttpClient 4.5版本。 将以下依赖项添加到您的项目中。
pom.xml 文件的内容如下 -
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.yiibai.httpclient.httmethods
http-get
1.0.0-SNAPSHOT
https://memorynotfound.com
httpclient - ${project.artifactId}
org.apache.httpcomponents
httpclient
4.5.2
maven-compiler-plugin
3.5.1
1.8
1.8
HttpClient分段上传请求示例
在这个例子中,我们将演示如何使用HttpClient 4.5来分段上传文件。 我们使用MultipartEntityBuilder创建一个HttpEntity。 当创建构建器时,添加一个二进制体 - 包含将要上传的文件以及一个文本正文。 接下来,使用RequestBuilder创建一个HTTP请求,并分配先前创建的HttpEntity。
文件:HttpClientMultipartUploadExample.java -
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
/**
* This example demonstrates the use of {@link HttpPost} request method.
* And sending Multipart Form requests
*/
public class HttpClientMultipartUploadExample {
public static void main(String... args) throws IOException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
File file = new File(HttpClientMultipartUploadExample.class.getResource("/java-duke.png").getFile());
String message = "This is a multipart post";
// build multipart upload request
HttpEntity data = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, file.getName())
.addTextBody("text", message, ContentType.DEFAULT_BINARY)
.build();
// build http request and assign multipart upload data
HttpUriRequest request = RequestBuilder
.post("http://httpbin.org/post")
.setEntity(data)
.build();
System.out.println("Executing request " + request.getRequestLine());
// Create a custom response handler
ResponseHandler responseHandler = response -> {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
String responseBody = httpclient.execute(request, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
}
}
}
所有先前的分段文件上传都包含在带有相应标头消息中。执行上面示例代码,得到以下结果 -
Executing request POST http://httpbin.org/post HTTP/1.1
----------------------------------------
{
"args": {},
"data": "",
"files": {
"upfile": "data:application/octet-stream;base64,iVBORw0KGgoAAAANSUhEUgAAAj....."
},
"form": {
"text": "This is a multipart post"
},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Connection": "close",
"Content-Length": "53697",
"Content-Type": "multipart/form-data; boundary=SqncIg58wNoviG6dWynFmxPJnvICdQg",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5.5 (Java/1.8.0_65)"
},
"json": null,
"origin": "112.67.166.104",
"url": "http://httpbin.org/post"
}