ResumeSDKUtil.java 2.65 KB
Newer Older
284718418@qq.com committed
1 2 3
package cn.timer.api.utils.resumesdk;

import cn.timer.api.dto.resumesdk.ResumeSdkDto;
4 5 6 7 8 9 10 11
import com.alibaba.fastjson.JSONObject;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
284718418@qq.com committed
12 13 14 15 16 17 18 19 20
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
21
import java.util.HashMap;
284718418@qq.com committed
22
import java.util.List;
23 24 25
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
284718418@qq.com committed
26 27 28 29 30 31 32 33 34

/**
 * @author wuqingjun
 * @email 284718418@qq.com
 * @date 2022/4/22
 */
@Component
public class ResumeSDKUtil {

35
    public String getResumeByCvParser(ResumeSdkDto resumeSdkDto, String appCode) throws Exception {
284718418@qq.com committed
36
        String url = "http://resumesdk.market.alicloudapi.com/ResumeParser";
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        // 设置头字段
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Authorization", "APPCODE " + appCode);
        httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
        httpPost.addHeader("Content-Type", "application/json");


        // 设置内容信息
        JSONObject json = new JSONObject();
        json.put("file_name", resumeSdkDto.getFile_name());	// 文件名
        json.put("file_cont", resumeSdkDto.getFile_cont());	// 经base64编码过的文件内容
        json.put("need_avatar", 0);		// 是否需要解析头像
        json.put("ocr_type", 1);		// 1为高级ocr
        StringEntity params = new StringEntity(json.toString(), Consts.UTF_8);
        httpPost.setEntity(params);

        // 发送请求
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(httpPost);

        // 处理返回结果
        return unicodeToCN(EntityUtils.toString(response.getEntity(), Consts.UTF_8));
    }
    /**
     * Unicode转 汉字字符串
     *
     * @param str
     * @return
     */
    private static String unicodeToCN(String str) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            str = str.replace(matcher.group(1), ch + "");
        }
        return str;
284718418@qq.com committed
75
    }
76

284718418@qq.com committed
77
}