栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java使用HttpClient发送post,get请求,附文件入参

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java使用HttpClient发送post,get请求,附文件入参

核心pom


            org.apache.httpcomponents
            httpclient
            4.5.3

 
            org.apache.httpcomponents
              httpmime
              4.5.3
//json转map
public Map JSONandMap(String param){
    Map userMap=new HashMap<>();
    JSONObject response = JSONObject.parseObject(param);
    for (Map.Entry entry : response.entrySet()) {
        userMap.put(entry.getKey(), entry.getValue());
    }
    return userMap;
}

//发送post请求的文件上传

public Map addfile(MultipartFile file)  {
    //创建http客户端
    CloseableHttpClient httpClient= HttpClientBuilder.create().build();
    //接收数据并返回
    Map userMap=new HashMap<>();
    String resultJson=null;
    //发送post请求传入地址
    HttpPost post=new HttpPost(file_url); //127.0.0.1:8080/xxxx/xxxx
    try {
        //上传文件 别的格式参考 httpEntity
        MultipartEntityBuilder builder=MultipartEntityBuilder.create();
        //参数分别为
        // files: 入参的键,
        // file.getInputStream(): 字节输入流
        // ContentType.MULTIPART_FORM_DATA: 设置内容类型
        //file.getOriginalFilename(): 文件名称
        builder.setCharset(Charset.forName("UTF-8")).addBinaryBody
                ("files", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, file.getOriginalFilename());
        //
        HttpEntity entity=builder.build();
        post.setEntity(entity);
        HttpResponse res = httpClient.execute(post);
        if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            // 返回json格式
            resultJson = EntityUtils.toString(res.getEntity());
            //JSONandMap 下面封装的JSON 转 map对象
            userMap=JSONandMap(resultJson);
             System.out.println(userMap);
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return  userMap;
}
//发送post json 请求
public Map pythonSF(Map map,String url) throws JsonProcessingException{
    String resultJson=null;
    Map userMap=new HashMap<>();
    
    CloseableHttpClient httpClient= HttpClientBuilder.create().build();
    HttpPost post=new HttpPost(url);
    String json = objectMapper.writeValueAsString(map);
    try {
        //UrlEncodedFormEntity entity=new UrlEncodedFormEntity(json, Consts);
        StringEntity s = new StringEntity(json);
        s.setContentEncoding("UTF-8");
        s.setContentType("application/json");
        post.setEntity(s);
        HttpResponse res = httpClient.execute(post);
        if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            resultJson = EntityUtils.toString(res.getEntity());
            userMap=JSONandMap(resultJson);
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return userMap;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/981124.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号