Bootstrap

后台调用接口传输multipartFile

对方接口样式

后台直接调用该接口

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>

;