身份证识别通过RestTemple进行的post请求。

通过RestTemple进行的post请求。其中的参数是file类型,
工具 idea 项目 mevan 技术 sringboot


```java
package com.guangda.CardApply;

import com.guangda.untils.HttpsClientRequestFactory;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.*;

/**
 * 身份证orc验证
 * 前台传递,照片。
 */
@RestController
public class SFZIdcardApply {
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/idcard/SFZapply", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject idcardApply(@RequestParam("myFile") MultipartFile myFile,String fileCategory) throws IOException {
//    public JSONObject idcardApply() throws MalformedURLException {
        if (Objects.isNull(myFile) || myFile.isEmpty()) {//判断非空
            // logger.error("文件为空");
            return null;
        }
        //第三方地址
        String url=" https:";
        //必须转换一下,否则请求的时候会报错
        URL url1=new URL(url);
        //第三方要求的参数
        boolean formSdk=false;
        Set<String> tempFilePath = new HashSet<>();
        //第三方接口生成的token
        String token=new ThirdApply().apply().getJSONObject("data").get("token").toString();
        RestTemplate template = new RestTemplate(new HttpsClientRequestFactory());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        if (null != myFile && !myFile.isEmpty()) {
            //生成临时的文件,完成传输后再删除
            File tempFile = new File( UUID.randomUUID().toString()+myFile.getOriginalFilename());
            tempFilePath.add(tempFile.getAbsolutePath());
            FileUtils.copyInputStreamToFile(myFile.getInputStream(), tempFile);
            FileSystemResource fileSystemResource = new FileSystemResource(tempFile);
            params.add("fileData", fileSystemResource);
        }
        params.add("formSdk", formSdk);
        params.add("fileCategory", fileCategory);
        params.add("token", token);
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(params,headers);
        ResponseEntity<JSONObject> response = template.postForEntity(url1.toString(), requestEntity, JSONObject.class);
        return response.getBody();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值