最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java?Post請求發(fā)送form-data表單參數(shù)詳細示例代碼

 更新時間:2025年07月05日 09:23:47   作者:胡德祿a  
POST請求是一種常見的網(wǎng)絡(luò)通信操作,用于向服務(wù)器發(fā)送數(shù)據(jù),這種請求通常用于上傳文件或者提交包含大量數(shù)據(jù)的表單,這篇文章主要介紹了Java?Post請求發(fā)送form-data表單參數(shù)的相關(guān)資料,需要的朋友可以參考下

Post請求發(fā)送form-data表單參數(shù)

一、pom文件引入依賴

        <!-- 添加 Apache HttpClient 依賴 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version> <!-- 請使用最新的穩(wěn)定版本 -->
        </dependency>

        <!-- 如果你需要使用 MIME 相關(guān)的實用工具,添加 mime4j 依賴 -->
        <dependency>
            <groupId>org.apache.james</groupId>
            <artifactId>apache-mime4j</artifactId>
            <version>0.6.1</version> <!-- 請使用最新的穩(wěn)定版本 -->
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.13</version> <!-- 請使用最新的版本號 -->
        </dependency>

二、工具類

package com.hn.bdzzhixun.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
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.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * @author lzn
 * @version :1.0
 *  2024/11/22 15:10
 */
@Slf4j
public class SendFileUtils {

    /**
     * 使用multipart/form-data方式傳輸文件
     * 發(fā)送文件方法
     * @param url 接口地址
     * @param file 文件
     */
    public static String sendMultipartFile(String url, File file,String dwbh) {
        //獲取HttpClient
        CloseableHttpClient client = getHttpClient();
        HttpPost httpPost = new HttpPost(url);
        fillMethod(httpPost,System.currentTimeMillis());

        // 請求參數(shù)配置
        RequestConfig requestConfig = RequestConfig
                .custom()
                .setSocketTimeout(60000)
                .setConnectTimeout(3000)
                .setConnectionRequestTimeout(3000)
                .build();
        httpPost.setConfig(requestConfig);
        String res = "";
        String fileName = file.getName();//文件名
        try {

            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setCharset(StandardCharsets.UTF_8);
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

            /*
              假設(shè)有兩個參數(shù)需要傳輸
              參數(shù)名:filaName 值 "文件名"
              參數(shù)名:file 值:file (該參數(shù)值為file對象)
             */
            //表單中普通參數(shù)
            builder.addPart("filaName ",new StringBody("來源", ContentType.create("text/plain", Consts.UTF_8)));

            // 表單中的文件參數(shù) 注意,builder.addBinaryBody的第一個參數(shù)要寫參數(shù)名
            builder.addBinaryBody("file", file, ContentType.create("multipart/form-data",Consts.UTF_8), fileName);

            builder.addTextBody("positionId",dwbh);

            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            HttpResponse response = client.execute(httpPost);// 執(zhí)行提交

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                // 返回響應結(jié)果
                res = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
            }else {
                res = "響應失敗";
                log.error("響應失??!");
            }
            return res;

        } catch (Exception e) {
            e.printStackTrace();
            log.error("調(diào)用HttpPost失??!" + e);
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                    log.error("關(guān)閉HttpPost連接失??!");
                }
            }
        }
        log.info("數(shù)據(jù)傳輸成功!!!!!!!!!!!!!!!!!!!!");
        return res;
    }

    /**
     * 獲取HttpClient
     */
    private static CloseableHttpClient getHttpClient(){
        SSLContext sslContext;
        try {
            sslContext = new SSLContextBuilder().loadTrustMaterial(null, (TrustStrategy) (arg0, arg1) -> true).build();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
                NoopHostnameVerifier.INSTANCE);
        return HttpClientBuilder.create().setSSLSocketFactory(sslConnectionSocketFactory).build();
    }

    /**
     * 添加頭文件信息
     */
    private static void fillMethod(HttpRequestBase requestBase, long timestamp){
        //此處為舉例,需要添加哪些頭部信息自行添加即可

        //設(shè)置時間戳,nginx,underscores_in_headers on;放到http配置里,否則nginx會忽略包含"_"的頭信息
        requestBase.addHeader("timestamp",String.valueOf(timestamp));
        System.out.println(Arrays.toString(requestBase.getAllHeaders()));
    }
}


總結(jié) 

到此這篇關(guān)于Java Post請求發(fā)送form-data表單參數(shù)的文章就介紹到這了,更多相關(guān)Java Post請求發(fā)送form-data參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java instanceof用法詳解及實例代碼

    Java instanceof用法詳解及實例代碼

    這篇文章主要介紹了Java instanceof用法詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • 如何在Java中優(yōu)雅地判空詳解

    如何在Java中優(yōu)雅地判空詳解

    這篇文章主要大家介紹了關(guān)于如何在Java中優(yōu)雅地判空的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-11-11
  • Java線程之join_動力節(jié)點Java學院整理

    Java線程之join_動力節(jié)點Java學院整理

    join() 定義在Thread.java中,下文通過源碼分享join(),需要的朋友參考下吧
    2017-05-05
  • maven中springboot-maven-plugin的5種打包方式

    maven中springboot-maven-plugin的5種打包方式

    本文主要介紹了maven中springboot-maven-plugin的5種打包方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-09-09
  • Kotlin 單例實例詳解

    Kotlin 單例實例詳解

    這篇文章主要介紹了Kotlin 單例實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • SpringBoot Caffeine+Redisson配置二級緩存實踐

    SpringBoot Caffeine+Redisson配置二級緩存實踐

    文章介紹了兩級緩存架構(gòu)的必要性,詳細描述了使用Redission進行SpringBoot緩存整合的方法,包括配置本地緩存、設(shè)置過期時間、開啟緩存功能、解決key相同cacheNames不同的問題以及修改自定義緩存管理器等內(nèi)容
    2026-05-05
  • Java中構(gòu)造方法set/get和toString的使用詳解

    Java中構(gòu)造方法set/get和toString的使用詳解

    這篇文章主要介紹了Java中構(gòu)造方法set/get和toString的使用詳解,構(gòu)造函數(shù)的最大作用就是創(chuàng)建對象時完成初始化,當我們在new一個對象并傳入?yún)?shù)的時候,會自動調(diào)用構(gòu)造函數(shù)并完成參數(shù)的初始化,需要的朋友可以參考下
    2019-07-07
  • Java內(nèi)存泄漏問題排查與解決

    Java內(nèi)存泄漏問題排查與解決

    大家好,本篇文章主要講的是Java內(nèi)存泄漏問題排查與解決,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • java中Supplier知識點總結(jié)

    java中Supplier知識點總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于java中Supplier知識點總結(jié)內(nèi)容,有興趣的朋友們可以學習下。
    2021-04-04
  • Springboot發(fā)送post請求的幾種方式總結(jié)

    Springboot發(fā)送post請求的幾種方式總結(jié)

    這篇文章主要為大家詳細介紹了Springboot發(fā)送post請求的幾種方式,文中的示例代碼講解詳細,對我們學習或工作有一定的幫助,感興趣的小伙伴可以了解一下
    2024-01-01

最新評論

广昌县| 河北区| 津南区| 沛县| 竹北市| 襄城县| 克东县| 佳木斯市| 新和县| 白朗县| 仁寿县| 海伦市| 宜宾市| 游戏| 孟津县| 正定县| 盐城市| 铅山县| 临潭县| 孙吴县| 中山市| 西充县| 平原县| 和平区| 定南县| 景德镇市| 磴口县| 抚宁县| 汝南县| 河曲县| 嘉峪关市| 东至县| 来安县| 都匀市| 二连浩特市| 分宜县| 肇东市| 固原市| 高邮市| 怀安县| 丰镇市|