本文主要介紹javapost請求數(shù)據(jù)(java發(fā)送帶有參數(shù)映射的post請求),下面一起看看javapost請求數(shù)據(jù)(java發(fā)送帶有參數(shù)映射的post請求)相關(guān)資訊。
話不多說,代碼導(dǎo)入org . apache . http . consts;導(dǎo)入org . apache . http . http entity;導(dǎo)入org . apache . http . httpresponse;導(dǎo)入org . apache . http . http status;導(dǎo)入org . apache . http . client . config . request config;導(dǎo)入org . apache . http . client . methods . http post;導(dǎo)入org . apache . http . entity . content type;導(dǎo)入org . apache . http . entity . mime . http multipartmode;導(dǎo)入org . apache . http . entity . mime . multipartentitybuilder;導(dǎo)入org . apache . http . entity . mime . content . string body;import org . apache . http . impl . client . closeable http client;導(dǎo)入org . apache . http . impl . client . http clients;導(dǎo)入org . apache . http . util . entity utils;導(dǎo)入org . spring framework . web . multipart . multipart file;導(dǎo)入java . io . io exception;導(dǎo)入java . nio . charset . standard charsets;導(dǎo)入java . util . map;公共類httpfileutil {/** *通過post調(diào)用第三方接口。以格式發(fā)送多部分文件數(shù)據(jù)-data * @param url post請求url * @param fileparamname文件參數(shù)名稱* @ param multipart file */public static string dopostform data(string url,string file paramname,multipart file multipart file,mapstring,string param map){//創(chuàng)建http實例closeable http client = http clients . create default;//創(chuàng)建httppost的實例http post = new http post(url);//請求參數(shù)配置request config = request config . custom。setsockettimeout (60000)。setconnecttimeout (60000)。setconnectionrequesttime out(10000)。build;http post . set config(request config);請嘗試{ multipartentitybuilder builder = multipartentitybuilder . create;builder . set charset(standard charsets。utf _ 8);builder . set mode(http multipartmode。瀏覽器_兼容);string filename = multipart file . getoriginalfilename;//file stream builder . addbinarybody(file param name,multipart file.getinputstream,cont: param map . entry set){ builder . add part(entry . getkey,new stringbody(entry.getvalue,content type . create( 文本/普通 ,常量。utf _ 8)));} httpentity entity = builder . build;httppost.setentity(實體);//執(zhí)行提交httpresponse = http client . execute(http post);如果(回應(yīng)。getstatusline。getstatuscode= = http status . sc _ ok){//返回string res = entity utils . tostring(response。getentity、standard charsets . utf _ 8);返回res} } catch(異常e){ e . printstacktrace;}最后{ if (httpclient!= null){ try { http client . close;} catch(io exception e){ e . printstacktrace;} } }返回null}}
標(biāo)簽:
參數(shù)文件
了解更多javapost請求數(shù)據(jù)(java發(fā)送帶有參數(shù)映射的post請求)相關(guān)內(nèi)容請關(guān)注本站點。