POST请求第三方接口(字符串+文件参数)

该博客展示了如何使用Apache HttpClient库来获取第三方接口的token并上传文件类型的参数。首先,创建一个HttpPost请求,然后构建一个MultipartEntityBuilder以添加文件名和参数。接着,将JSON对象转换为字节数组输入流,并添加到请求体中。最后,执行HTTP请求,处理响应并获取token。
摘要由CSDN通过智能技术生成

依赖


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

我这里是获取第三方接口的token,上传文件类型参数

    public static String getToken(){
        logger.info("token请求时间:" + DateUtil.formatDate(new Date(), DateUtil.PATTERN_YYYY_MM_DD_HH_MM_SS));
        InputStream in = null;
        String result = "";
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost postTool = new HttpPost(GZ_BASE_URL);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            //文件名
            String fileName = platform+"_"+"TOKEN"+"_"+"ACCESS"+"_"+"REQ"+"_"+JwPushUtil.dateToLongyyyyMMddHHmmssSSS(new Date())+".json";
            builder.addTextBody("filename", fileName, ContentType.TEXT_PLAIN);
            JSONObject jsonObject = new JSONObject();
            //参数添加到集合
            jsonObject.put("platform",platform);
            jsonObject.put("key",GZCompanyId);
            //我这里是直接将json数据转成流,如果需要从本地读取数据的话可以先将文件读到文件流中
            //String sTestsetFile="d://DDWLJH6D_TOKEN_ACCESS_REQ_20211202161720637.json";
           //InputStream inputStream = new FileInputStream(sTestsetFile); 
              in  =   new ByteArrayInputStream(jsonObject.toString().getBytes());
            builder.addBinaryBody(
                    "binFile",
                    in,
                    ContentType.APPLICATION_OCTET_STREAM,
                    fileName
            );

            //参数集合
            postTool.addHeader("binfile-auth","DDWLJH6D");
            HttpEntity multipart = builder.build();
            postTool.setEntity(multipart);

            CloseableHttpResponse response = httpClient.execute(postTool);
            HttpEntity responseEntity = response.getEntity();
            String sResponse= EntityUtils.toString(responseEntity, "UTF-8");
            //打印请求返回的结果
            System.out.println("Post 返回结果"+sResponse);
            JSONObject json = JSONObject.parseObject(sResponse);
            result = (String) json.get("token");
            // 把文件加到HTTP的post请求中
            //File f = new File(sTestsetFile);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        System.out.println("返回数据->"+result);
        return result;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值