0
点赞
收藏
分享

微信扫一扫

使用form-data传输文件 案例

雨鸣静声 2022-02-21 阅读 158
json
//封装参数
        LoanSucceedInfoParam loanSucceedInfoParam = setLoanSucceedInfoParam(orderNumber);

        List<RmAttachmentParam> rmAttachmentParamList = new ArrayList<>();
        //搜索所有附件
        List<Attachment> attachmentList = attachmentService.getListByOrderNumber(orderNumber);
        for (Attachment attachment : attachmentList) {
            RmAttachmentParam rmAttachmentParam = new RmAttachmentParam();
            BeanUtils.copyProperties(attachment,rmAttachmentParam);
            rmAttachmentParamList.add(rmAttachmentParam);
        }
        log.info("附件信息:{}",JacksonUtils.toJson(rmAttachmentParamList));

        //调用接口
        CloseableHttpClient httpClient = HttpClientPoolUtils.getHttpClient();
        HttpPost httpPost = new HttpPost(fundHost + fundSaveSuccessInfo);
        log.info("接口提交-传输放款成功数据给贷后系统,地址:[{}]", fundHost + fundSaveSuccessInfo);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("loanSucceedInfoDto",JacksonUtils.toJson(loanSucceedInfoParam), ContentType.APPLICATION_JSON);
        builder.addTextBody("rmAttachmentDtoList",JacksonUtils.toJson(rmAttachmentParamList), ContentType.APPLICATION_JSON);

        for (Attachment attachment : attachmentList) {
            File file = new File(fileuploadPath+attachment.getUrl());
            builder.addBinaryBody(file.getName(),file);
        }

        HttpEntity build = builder.build();
        httpPost.setEntity(build);

        ThirdpartyRequestRecord thirdpartyRequestRecord = new ThirdpartyRequestRecord();
        thirdpartyRequestRecord.setUrl(fundHost + fundSaveSuccessInfo);
        Map<String,Object> map = new HashMap<>();
        map.put("loanSucceedInfoDto",loanSucceedInfoParam);
        map.put("rmAttachmentDtoList",rmAttachmentParamList);
        thirdpartyRequestRecord.setRequest(JacksonUtils.toJson(map));
        thirdpartyRequestRecord.setType(ThirdpartyTypeEnum.HOME_AFTER_LOAN.getCode());

        try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            thirdpartyRequestRecord.setResponse(JSON.toJSON(httpResponse).toString());
            log.info("贷后接口返回状态码:{}",statusCode);
            if (statusCode == HttpStatus.SC_OK) {
                String response = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
                thirdpartyRequestRecord.setResponse(JSON.toJSON(response).toString());
                log.info("订单[{}]推送数据返回结果:[{}]", orderNumber, response);
                String code = JSON.parseObject(response).getString("code");
                String msg = JSON.parseObject(response).getString("msg");
                if (!"10000".equals(code)){
                    log.error("客户订单号[{}]数据推送至贷后系统异常", orderNumber);
                }
            }
            //保存返回结果信息
            thirdpartyRequestRecordService.insert(thirdpartyRequestRecord);
        }catch (Exception e){
            log.error("客户订单号[{}]数据推送至贷后系统异常", orderNumber, e);
        }
举报

相关推荐

0 条评论