后台调用接口传输multipartFile

这篇博客详细介绍了如何使用Apache HttpClient库调用后台文件上传接口的过程,包括设置请求参数、构建Http实体及处理响应。代码示例中展示了上传多个文件的实现方式,并引入了httpmime依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对方接口样式

后台直接调用该接口

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.HttpMultipartMode;


// 创建Http实例
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建HttpPost实例
        HttpPost httpPost = new HttpPost(hdsfip + uploadUrl+ "?zybm=123&fydm=123" );
        // 请求参数配置
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000)
                .setConnectionRequestTimeout(10000).build();
        httpPost.setConfig(requestConfig);
        httpPost.setHeader("Authorization","Bearer "+tokenVO.getAccess_token());
        try {
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setCharset(StandardCharsets.UTF_8);
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            for (File file : files) {
                String fileName = file.getName();
                System.err.println(file);
                System.err.println(fileName);
                System.err.println("==============================");
                // 文件流
                builder.addBinaryBody("file", new FileInputStream(file), ContentType.MULTIPART_FORM_DATA, fileName);
            }
            //表单中其他参数,如果没有其他参数可以注释该部分
//            for(Map.Entry<String, String> entry: paramMap.entrySet()) {
//                builder.addPart(entry.getKey(),new StringBody(entry.getValue(), ContentType.create("text/plain", Consts.UTF_8)));
//            }
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            // 执行提交
            CloseableHttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                // 返回
                String result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                System.err.println("文件上传:   "+result);
                HdsfResultVO resultVO = JSONObject.parseObject(result, HdsfResultVO.class);
                return resultVO;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

引包

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.2</version>
            <scope>compile</scope>
        </dependency>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值