linux http 封装,使用 httpclient 上传文件方法的封装

本人使用 httpclient 进行接口测试的过程中,遇到了上传文件的接口,之前的文章已经完成了二进制流上传图片的代码,但是还没有封装成固定的使用方法,今天分享一下封装后的方法,供大家参考。

/**

* 设置二进制流实体,params 里面参数值为 file

*

* @param httpPost

* httpPsot 请求

* @param params

* 请求参数

* @param file

* 文件

*/

public void setMultipartEntityEntity(HttpPost httpPost, JSONObject params, File file) {

String fileName = getFileName(file);

InputStream inputStream = null;

try {

inputStream = new FileInputStream(file);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

Iterator keys = params.keys();// 遍历 params 参数和值

MultipartEntityBuilder builder = MultipartEntityBuilder.create();// 新建builder对象

while (keys.hasNext()) {

String key = keys.next();

String value = params.getString(key);

if (value.equals("file")) {

builder.addBinaryBody(key, inputStream, ContentType.create("multipart/form-data"), fileName);// 设置流参数

} else {

StringBody body = new StringBody(value, ContentType.create("text/plain", Consts.UTF_8));// 设置普通参数

builder.addPart(key, body);

}

}

HttpEntity entity = builder.build();// 生成entity

httpPost.setEntity(entity);// 设置 entity

}

此方法仅针对 Linux 系统,因为 Windows 系统在文件路径中用的 “\”,在代码里是 “\” 所以 Windows 系统的朋友得注意力。

技术类文章精选

非技术文章精选

大咖风采

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用HttpClient上传文件的示例代码: ``` import java.io.File; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; public class FileUploader { public static void main(String[] args) { String url = "http://example.com/upload"; String filePath = "/path/to/file.txt"; // 创建HttpClient CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建HttpPost请求 HttpPost httpPost = new HttpPost(url); // 创建multipart/form-data实体 File file = new File(filePath); HttpEntity httpEntity = MultipartEntityBuilder.create() .addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName()) .build(); // 设置请求实体 httpPost.setEntity(httpEntity); try { // 执行请求 HttpResponse response = httpClient.execute(httpPost); // 输出响应结果 System.out.println(response.getStatusLine().getStatusCode()); System.out.println(response.getEntity().getContent().toString()); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭HttpClient try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } } ``` 在上面的示例代码中,我们使用了Apache HttpClient 4.x库来发送POST请求,并上传文件。我们首先创建了一个CloseableHttpClient实例,然后创建一个HttpPost实例,并设置请求实体为multipart/form-data类型的实体。 在实体中,我们添加了一个二进制文件体,它将文件添加到请求中。在这个例子中,我们使用了File类来表示文件,并用ContentType.DEFAULT_BINARY创建了一个ContentType实例。 最后,我们执行请求,并输出响应结果。注意,在使用HttpClient时,我们需要手动关闭HttpClient实例,以释放连接和资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值