httpclient5 上传文件

api ‘org.apache.httpcomponents.client5:httpclient5:5.2.2’
api ‘org.apache.commons:commons-compress:1.24.0’

import lombok.SneakyThrows;
import org.apache.commons.compress.utils.FileNameUtils;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.mime.FileBody;
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.springframework.util.Assert;

import java.io.File;
import java.nio.charset.StandardCharsets;

public class Test {

    @SneakyThrows
    public static void main(String[] args) {
        File file = new File("C:\\Users\\admin\\Desktop\\temp.mp4");
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try(httpClient) {
            HttpPost httpPost = new HttpPost("https://xxx.com//upload");
            httpPost.setHeader("token", "xxx");
            MultipartEntityBuilder builder  = MultipartEntityBuilder.create();
            builder.addTextBody("account", "1233221");
            // 文件名
            builder.addBinaryBody("filename", FileNameUtils.getBaseName(file.getName()).getBytes(StandardCharsets.UTF_8));
            // 文件
            builder.addPart("file", new FileBody(file));
            httpPost.setEntity(builder.build());
            CloseableHttpResponse response = httpClient.execute(httpPost);
            Assert.isTrue(response.getCode() == HttpStatus.SC_OK, response.getReasonPhrase());
            String jsonStr = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8.displayName());
            System.out.println(jsonStr);
        }
    }
}
  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 WPF 中使用 HttpClient 上传文件的步骤如下: 1. 引入必要的命名空间: ```csharp using System; using System.IO; using System.Net.Http; ``` 2. 创建一个 HttpClient 对象: ```csharp var httpClient = new HttpClient(); ``` 3. 创建一个 MultipartFormDataContent 对象,并添加文件内容: ```csharp var content = new MultipartFormDataContent(); var fileContent = new StreamContent(File.OpenRead(filePath)); content.Add(fileContent, "file", Path.GetFileName(filePath)); ``` 这里的 `filePath` 是要上传的文件路径。 4. 发起 POST 请求并上传文件: ```csharp var response = await httpClient.PostAsync(apiUrl, content); ``` 这里的 `apiUrl` 是上传文件的 API 地址。 5. 处理响应结果: ```csharp if (response.IsSuccessStatusCode) { // 文件上传成功 // 处理成功的逻辑 } else { // 文件上传失败 // 处理失败的逻辑 } ``` 完整的代码示例: ```csharp using System; using System.IO; using System.Net.Http; public async void UploadFile(string filePath, string apiUrl) { try { using (var httpClient = new HttpClient()) { using (var content = new MultipartFormDataContent()) { var fileContent = new StreamContent(File.OpenRead(filePath)); content.Add(fileContent, "file", Path.GetFileName(filePath)); var response = await httpClient.PostAsync(apiUrl, content); if (response.IsSuccessStatusCode) { // 文件上传成功 // 处理成功的逻辑 } else { // 文件上传失败 // 处理失败的逻辑 } } } } catch (Exception ex) { // 异常处理 } } ``` 请注意,这里的上传文件是使用 HTTP POST 请求方式进行的,你需要根据你的实际需求来调整代码。另外,还可以根据 API 的要求设置其他的请求头或请求参数。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值