httpclient远程调用接口Demo

导入Jar包

		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpmime</artifactId>
			<version>4.4</version>
		</dependency>

第一种方式上传文件(httpclient)

 public static void main(String[] args) {
        try {
            String filePath = "C:\\Users\\Dell\\Desktop\\做好的模板\\使用教程.txt";
            InputStream is = new FileInputStream(new File(filePath));
            String fileName ="aaa";
            String uploadURL = "http://localhost:8083/aaa"; //接收文件的服务器地址
            //创建HttpClient
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(uploadURL);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();  //上传文件
            /*这是绑定上传文件接口*/
            builder.addBinaryBody("file", is, ContentType.DEFAULT_BINARY,fileName);
            //还可以设置其他参数
            builder.addTextBody("jsonObject","jsonObject");
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            //执行提交
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            if(responseEntity != null){
                //将响应的内容转换成字符串
                String result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
                System.out.println(result);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

第二种方式上传文件(spring)

  /**
     * 调用接口
     * @param vo 发送数据
     * @param filePath 文件本地位置
     * @param riu 远程接口
     */
    public static void ocrDiscern(String jsonObject, String filePath, String riu) {

        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);
        //设置请求体,注意是LinkedMultiValueMap
        FileSystemResource fileSystemResource = new FileSystemResource(filePath);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        //设置发送文件和其它参数
        form.add("multipartFile", fileSystemResource);
        form.add("jsonObject", jsonObject);
        form.add("AppId", "2002100");
        org.springframework.http.HttpEntity<MultiValueMap<String, Object>> files = new org.springframework.http.HttpEntity<>(form, headers);
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.postForObject(riu, files, String.class);
        System.out.println(result);
    }

三.发送JSON数据

/**
     * 调用接口
     * @param path 远程接口
     * @param obj 发送数据
     * @return 返回结果
     */
    public static String callingInterface(String path,Object obj) throws Exception{
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(path);
        httpPost.setEntity(new StringEntity(JSON.toJSONString(obj),
                ContentType.create("application/json", "UTF-8")));
        //执行
        CloseableHttpResponse execute = httpClient.execute(httpPost);
        HttpEntity entity = execute.getEntity();
        String str = EntityUtils.toString(entity);
        httpPost.clone();
        httpClient.close();
        System.out.println(str);
        return str;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值