本文主要介绍javapost请求数据(java发送带有参数映射的post请求),下面一起看看javapost请求数据(java发送带有参数映射的post请求)相关资讯。
话不多说,代码导入org . apache . http . consts;导入org . apache . http . http entity;导入org . apache . http . httpresponse;导入org . apache . http . http status;导入org . apache . http . client . config . request config;导入org . apache . http . client . methods . http post;导入org . apache . http . entity . content type;导入org . apache . http . entity . mime . http multipartmode;导入org . apache . http . entity . mime . multipartentitybuilder;导入org . apache . http . entity . mime . content . string body;import org . apache . http . impl . client . closeable http client;导入org . apache . http . impl . client . http clients;导入org . apache . http . util . entity utils;导入org . spring framework . web . multipart . multipart file;导入java . io . io exception;导入java . nio . charset . standard charsets;导入java . util . map;公共类httpfileutil {/** *通过post调用第三方接口。以格式发送多部分文件数据-data * @param url post请求url * @param fileparamname文件参数名称* @ param multipart file */public static string dopostform data(string url,string file paramname,multipart file multipart file,mapstring,string param map){//创建http实例closeable http client = http clients . create default;//创建httppost的实例http post = new http post(url);//请求参数配置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(实体);//执行提交httpresponse = http client . execute(http post);如果(回应。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}}
标签:
参数文件
了解更多javapost请求数据(java发送带有参数映射的post请求)相关内容请关注本站点。