java http 分段下载_HttpClient分段上传

本篇文章帮大家学习HttpClient分段上传,包含了HttpClient分段上传使用方法、操作技巧、实例演示和注意事项,有一定的学习价值,大家可以用来参考。

使用HttpClient,我们可以执行分段上传,即可以在较小的部分上传较大的对象。在本章中,通过上传简单的文本文件来演示HTTP客户端中的分段上传。

通常,任何分段上传都包含三个部分。

启动上传

上传对象部分

完成分段上传

对于使用HttpClient的分段上传,我们需要按照以下步骤 -

创建一个多部分构建器。

添加所需的部件。

完成构建并获得多部分HttpEntity。

通过设置上面的多部分实体来构建请求。

执行请求。

以下是使用HttpClient库上载多部分实体的步骤。

第1步 - 创建一个HttpClient对象HttpClients类的createDefault()方法返回类CloseableHttpClient的对象,该对象是HttpClient接口的基本实现。使用此方法,创建一个HttpClient对象 -

//Creating CloseableHttpClient object

CloseableHttpClient httpclient = HttpClients.createDefault();

第2步 - 创建FileBody对象

FileBody类表示由文件支持的二进制正文部分。通过传递File对象和表示内容类型的ContentType对象来实例化此类。

//Creating a File object

File file = new File("sample.txt");

//Creating the FileBody object

FileBody filebody = new FileBody(file, ContentType.DEFAULT_BINARY);

第3步 - 创建一个MultipartEntityBuilder类对象

MultipartEntityBuilder类用于构建多部分HttpEntity对象。使用create()方法(同一个类)创建对象。

//Creating the MultipartEntityBuilder

MultipartEntityBuilder entitybuilder = MultipartEntityBuilder.create();

第4步 - 设置模式MultipartEntityBuilder有三种模式:STRICT,RFC6532和BROWSER_COMPATIBLE。使用setMode()方法将其设置为所需模式。

//Setting the mode

entitybuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

第5步 - 添加各种所需的部分使用addTextBody(),addPart()和addBinaryBody()方法,可以将简单文本,文件,流和其他对象添加到MultipartBuilder对象。使用这些方法添加所需的内容。

//Adding text

entitybuilder.addTextBody("sample_text", "This is the text part of our file");

//Adding a file

entitybuilder.addBinaryBody("image", new File("logo.png"));

第6步 - 构建单个实体可以使用MultipartEntityBuilder类的build()方法将所有这些部件构建到单个实体。使用此方法,将所有部件构建到单个HttpEntity中。

//Building a single entity using the parts

HttpEntity mutiPartHttpEntity = entityBuilder.build();

第7步 - 创建RequestBuilder对象

RequestBuilder类用于通过向其添加参数来构建请求。如果请求的类型为PUT或POST,则它将参数作为URL编码实体添加到请求中。

使用post()方法创建RequestBuilder对象(类型为POST)。并将想要发送请求的Uri作为参数传递给它。

//Building the post request object

RequestBuilder reqbuilder = RequestBuilder.post("http://httpbin.org/post");

第8步 - 将实体对象设置为RequestBuilder使用RequestBuilder类的setEntity()方法将上面创建的multipart实体设置为RequestBuilder。

//Setting the entity object to the RequestBuilder

reqbuilder.setEntity(mutiPartHttpEntity);

第9步 - 构建HttpUriRequest使用RequestBuilder类的build()方法构建HttpUriRequest请求对象。

//Building the request

HttpUriRequest multipartRequest = reqbuilder.build();

第10步 - 执行请求使用execute()方法,执行上一步中构建的请求(绕过请求作为此方法的参数)。

//Executing the request

HttpResponse httpresponse = httpclient.execute(multipartRequest);

示例

以下示例演示了如何使用HttpClient库发送多部分请求。在此示例中,尝试发送由文件支持的分段请求。

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

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.entity.mime.content.FileBody;

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;

import java.net.URISyntaxException;

public class MultipartUploadExample {

public static void main(String args[]) throws Exception{

//Creating CloseableHttpClient object

CloseableHttpClient httpclient = HttpClients.createDefault();

//Creating a file object

File file = new File("sample.txt");

//Creating the FileBody object

FileBody filebody = new FileBody(file, ContentType.DEFAULT_BINARY);

//Creating the MultipartEntityBuilder

MultipartEntityBuilder entitybuilder = MultipartEntityBuilder.create();

//Setting the mode

entitybuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

//Adding text

entitybuilder.addTextBody("sample_text", "This is the text part of our file");

//Adding a file

entitybuilder.addBinaryBody("image", new File("logo.png"));

//Building a single entity using the parts

HttpEntity mutiPartHttpEntity = entitybuilder.build();

//Building the RequestBuilder request object

RequestBuilder reqbuilder = RequestBuilder.post("http://httpbin.org/post");

//Set the entity object to the RequestBuilder

reqbuilder.setEntity(mutiPartHttpEntity);

//Building the request

HttpUriRequest multipartRequest = reqbuilder.build();

//Executing the request

HttpResponse httpresponse = httpclient.execute(multipartRequest);

//Printing the status and the contents of the response

System.out.println(EntityUtils.toString(httpresponse.getEntity()));

System.out.println(httpresponse.getStatusLine());

}

}

执行上面示例代码,得到以下结果:

{

"args": {},

"data": "",

"files": {

"image": "data:application/octets66PohrH3IWNk1FzpohfdXPIfv9X3490FGcuXsHn9X0piCwomF/xdgADZ9GsfSyvLYAAAAAE

lFTkSuQmCC"

},

"form": {

"sample_text": "This is the text part of our file"

},

"headers": {

"Accept-Encoding": "gzip,deflate",

"Connection": "close",

"Content-Length": "11104",

"Content-Type": "multipart/form-data;

boundary=UFJbPHT7mTwpVq70LpZgCi5I2nvxd1g-I8Rt",

"Host": "httpbin.org",

"User-Agent": "Apache-HttpClient/4.5.6 (Java/1.8.0_91)"

},

"json": null,

"origin": "217.216.100.180",

"url": "http://httpbin.org/post"

}

HTTP/1.1 200 OK

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值