apache-httpclient多文件上传支持Input

本文介绍如何在项目中引入Maven依赖,并演示了使用httpclient和httpmime库进行文件上传的方法,包括设置请求头、处理多种类型的文件上传及参数传递。

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

引入maven依赖

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.3.6</version>
        </dependency>

demo:

public String postForUpload(String url, Map<String, String> headers, Map<String, Object> files, String fileName,
                                       Map<String, String> params) {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        if(!CollectionUtils.isEmpty(headers)) {
            for (Map.Entry<String, String> entity : headers.entrySet()) {
                post.setHeader(entity.getKey(), entity.getValue());
            }
        }
        try {
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            builder.setCharset(StandardCharsets.UTF_8);
            if(!CollectionUtils.isEmpty(files)) {
                //上传的文件体
                for (String key : files.keySet()) {
                    if(files.get(key) instanceof File) {
                        builder.addBinaryBody(key, (File) files.get(key));
                    }
                    if(files.get(key) instanceof InputStream) {
                        builder.addBinaryBody(key, (InputStream) files.get(key), ContentType.MULTIPART_FORM_DATA, fileName);
                    }
                    if(files.get(key) instanceof byte[]) {
                        builder.addBinaryBody(key, (byte[]) files.get(key), ContentType.MULTIPART_FORM_DATA, fileName);
                    }
                }
            }

            if(!CollectionUtils.isEmpty(params)) {
                for (Map.Entry<String, String> entity : params.entrySet()) {
                    if(StringUtils.isNotBlank(entity.getValue())) {
                        builder.addTextBody(entity.getKey(), entity.getValue());
                    }
                }
            }
            HttpEntity entity = builder.build();
            post.setEntity(entity);
            HttpResponse response = client.execute(post);

            // 设置连接超时时间
            int timeout = 5000;
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout)
                    .setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
            post.setConfig(requestConfig);

            entity = response.getEntity();
            if(null != entity) {
                return EntityUtils.toString(entity, Charset.forName(DEFAULT_CHARSET));
            }
        }catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值