首頁 > 軟體

Java傳送form-data請求實現檔案上傳

2022-06-23 14:00:40

如何使用Java傳送form-data格式的請求上傳multipart檔案?,供大家參考,具體內容如下

封裝了以下工具類:

package com.leeyaonan.clinkz.common.util;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.util.CollectionUtils;

/**
 * HttpUtils
 *
 * @author Rot
 * @date 2021/10/15 17:45
 */
@Slf4j
public class HttpUtils {

    /**
     * 從連線池中獲取連線的超時時間--10s
     */
    private static int connectionRequestTimeout = 10000;
    /**
     * 使用者端和伺服器建立連線的超時時間--握手連線時間--10s
     */
    private static int connectTimeout = 60000;
    /**
     * 從對方服務接受響應流的時間
     */
    private static int socketTimeout = 60000;
    /**
     * 連線池最大連線數
     */
    private static int maxTotal = 800;
    /**
     * 每個主機的並行
     */
    private static int maxPerRoute = 20;
    private static PoolingHttpClientConnectionManager connectionManager = null;
    private static CloseableHttpClient httpClient;

    public static CloseableHttpClient getClient() {
        return httpClient;
    }

    static {
        log.info("初始化http connection 連線池 ...");
        try {
            // 設定同時支援 HTTP 和 HTPPS
            SSLContextBuilder builder = new SSLContextBuilder();
            builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
            SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(builder.build());
            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslConnectionSocketFactory).build();
            connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        } catch (Exception e) {
            log.error("初始化http 連線池異常", e);
            connectionManager = new PoolingHttpClientConnectionManager();
        }
        //連線池統一設定
        connectionManager.setMaxTotal(maxTotal);
        connectionManager.setDefaultMaxPerRoute(maxPerRoute);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout).build();

        //不做重試功能
        HttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(0, false);

        httpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).setRetryHandler(retryHandler).build();

        ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1);
        scheduledExecutorService.scheduleWithFixedDelay(() -> {
            connectionManager.closeExpiredConnections();
            connectionManager.closeIdleConnections(20, TimeUnit.SECONDS);
            log.info("回收過期的http連線完成 status:{}", connectionManager.getTotalStats());
        }, 30, 120, TimeUnit.SECONDS);

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            log.info("關閉 httpClient 連線");
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                log.error("關閉 httpClient 異常", e);
            }
        }));
    }

    /**
     * post請求提交form-data上傳檔案
     *
     * @param url
     * @param headers 請求頭
     * @return
     */
    public static String doPostUploadFile(String url, Map<String, String> headers, File file) {
        HttpPost httpPost = new HttpPost(url);
        packageHeader(headers, httpPost);
        String fileName = file.getName();

        CloseableHttpResponse response = null;

        String respContent = null;

        long startTime = System.currentTimeMillis();

        // 設定請求頭 boundary邊界不可重複,重複會導致提交失敗
        String boundary = "-------------------------" + UUID.randomUUID().toString();
        httpPost.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);

        // 建立MultipartEntityBuilder
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        // 設定字元編碼
        builder.setCharset(StandardCharsets.UTF_8);
        // 模擬瀏覽器
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        // 設定邊界
        builder.setBoundary(boundary);
        // 設定multipart/form-data流檔案
        builder.addPart("multipartFile", new FileBody(file));
        // application/octet-stream代表不知道是什麼格式的檔案
        builder.addBinaryBody("media", file, ContentType.create("application/octet-stream"), fileName);

        HttpEntity entity = builder.build();
        httpPost.setEntity(entity);

        try {
            response = httpClient.execute(httpPost);
            if (response != null && response.getStatusLine() != null && response.getStatusLine().getStatusCode() < 400) {
                HttpEntity he = response.getEntity();
                if (he != null) {
                    respContent = EntityUtils.toString(he, "UTF-8");
                }
            } else {
                log.error("對方響應的狀態碼不在符合的範圍內!");
                throw new RuntimeException();
            }
            return respContent;
        } catch (Exception e) {
            log.error("網路存取異常,請求url地址={},響應體={},error={}", url, response, e);
            throw new RuntimeException();
        } finally {
            log.info("統一外網請求引數列印,post請求url地址={},響應={},耗時={}毫秒", url, respContent, (System.currentTimeMillis() - startTime));
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                log.error("請求連結釋放異常", e);
            }
        }
    }

    /**
     * 封裝請求頭
     *
     * @param paramsHeads
     * @param httpMethod
     */
    private static void packageHeader(Map<String, String> paramsHeads, HttpRequestBase httpMethod) {
        if (!CollectionUtils.isEmpty(paramsHeads)) {
            Set<Map.Entry<String, String>> entrySet = paramsHeads.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                httpMethod.setHeader(entry.getKey(), entry.getValue());
            }
        }
    }

}

maven依賴:

<!--http-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.12</version>
</dependency>

核心部分:

// 設定請求頭 boundary邊界不可重複,重複會導致提交失敗
String boundary = "-------------------------" + UUID.randomUUID().toString();
        httpPost.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);

        // 建立MultipartEntityBuilder
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        // 設定字元編碼
        builder.setCharset(StandardCharsets.UTF_8);
        // 模擬瀏覽器
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        // 設定邊界
        builder.setBoundary(boundary);
        // 設定multipart/form-data流檔案
        builder.addPart("multipartFile", new FileBody(file));
        // application/octet-stream代表不知道是什麼格式的檔案
        builder.addBinaryBody("media", file, ContentType.create("application/octet-stream"), fileName);

        HttpEntity entity = builder.build();
        httpPost.setEntity(entity);

注意:這裡的builder.addPart("multipartFile", new FileBody(file));,multipartFile對應form表單的欄位名稱,如果介面更改了欄位名稱,這裡也需要更改

比如我有一個介面是這樣定義的:

@PostMapping("/xxx")
    public void test(@RequestParam(value = "abc") MultipartFile file) {
        ...
    }

那麼使用上述工具請求該介面的時候,就需要將

builder.addPart("multipartFile", new FileBody(file));

改為

builder.addPart("abc", new FileBody(file));

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com