下面我将详细讲解使用HttpClient实现文件上传下载方法的完整攻略。
一、引入HttpClient依赖
- 在maven中添加HttpClient依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.11</version>
</dependency>
- 下载HttpClient的jar包,加入项目依赖中。
二、文件上传
- 创建HttpClient
CloseableHttpClient httpClient = HttpClients.createDefault();
- 创建Post请求
HttpPost httpPost = new HttpPost(url);
- 创建MultipartEntityBuilder对象
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
- 设置参数和文件
builder.setCharset(Charset.defaultCharset());
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", file);
builder.addTextBody("params", params);
- 构建HttpEntity对象
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
- 执行请求
CloseableHttpResponse response = httpClient.execute(httpPost);
完整示例代码:
public static void uploadFile(String url, File file, String params) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(Charset.defaultCharset());
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", file);
builder.addTextBody("params", params);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
System.out.println("上传成功");
} else {
System.out.println("上传失败");
}
response.close();
httpClient.close();
}
三、文件下载
- 创建HttpClient
CloseableHttpClient httpClient = HttpClients.createDefault();
- 创建HttpGet请求
HttpGet httpGet = new HttpGet(url);
- 执行请求
CloseableHttpResponse response = httpClient.execute(httpGet);
- 读取response的数据
InputStream inputStream = response.getEntity().getContent();
byte[] bytes = new byte[1024];
int len;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(bytes)) != -1) {
byteArrayOutputStream.write(bytes, 0, len);
}
byte[] data = byteArrayOutputStream.toByteArray();
- 将数据写入文件
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(data);
fileOutputStream.flush();
fileOutputStream.close();
完整示例代码:
public static void downloadFile(String url, File file) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream inputStream = response.getEntity().getContent();
byte[] bytes = new byte[1024];
int len;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(bytes)) != -1) {
byteArrayOutputStream.write(bytes, 0, len);
}
byte[] data = byteArrayOutputStream.toByteArray();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(data);
fileOutputStream.flush();
fileOutputStream.close();
}
response.close();
httpClient.close();
}
至此,使用HttpClient实现文件的上传下载方法的攻略讲解结束。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:使用HttpClient实现文件的上传下载方法 - Python技术站