liujie
2026-06-09 c96fbe6b0aeb413a17c429133ee6d81d138144b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.xiaopeng.trafficControl.utils;
 
 
import cn.hutool.core.io.FileUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaopeng.trafficControl.config.MinistryTransportProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
 
/**
 * 交通部接口对接
 */
@Slf4j
@Component
public class MinistryOfTransport {
 
    /**
     * 公司名称:广州易点智慧出行科技有限公司
     *
     * 平台简称:“有鹏出行”
     * 标识:XPQCB67N
     * key:gEhROt7lg4DEBanR
     *
     * 对接ip:127.0.0.1,125.71.236.64,47.98.173.145,116.62.136.48,39.108.249.22,47.105.208.135,47.112.126.81,114.55.179.11,39.170.77.50,120.26.242.215
     *
     * 接口地址:https://wycapi.jtj.gz.gov.cn/api/app/common/binapi
     */
    @Value("${xp.file.path}")
    private String filePath;
 
    private final MinistryTransportProperties ministryTransportProperties;
 
    public MinistryOfTransport(MinistryTransportProperties ministryTransportProperties) {
        this.ministryTransportProperties = ministryTransportProperties;
    }
 
 
    /**
     * 获取token
     * @return
     */
    public String getToken(){
        //通过上传文件的方式获取token
        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
        String fileName = ministryTransportProperties.getPlatform() + "_TOKEN_ACCESS_REQ_" + format + ".json";
        File file = FileUtil.file(filePath + fileName);
        if(!file.exists()){
            file.getParentFile().mkdirs();
            try {
                file.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            file.setReadable(true, false);//设置读取权限
            file.setWritable(true, false);//设置写入权限
        }
        JSONObject body = new JSONObject();
        JSONArray items = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("platform", ministryTransportProperties.getPlatform());
        jsonObject.put("key", ministryTransportProperties.getKey());
        items.add(jsonObject);
        body.put("items", items);
        log.info("获取token请求参数已生成");
        file = FileUtil.writeUtf8String(body.toJSONString(), file);
        String md5 = MD5.create().digestHex(file);
        HttpRequest post = HttpUtil.createPost(ministryTransportProperties.getUrl());
        post.contentType("multipart/form-data;boundary=inputstream");
        post.header("binfile-md5", md5);
        post.header("binfile-gzip", "false");
        post.header("binfile-auth", "5cd4be8cf03842a6b4c3246011e3482e");
        post.form("binFile", file);
        post.form("filename", fileName);
        HttpResponse execute = post.execute();
        if(!execute.isOk()){
            log.info("获取token失败,status={}", execute.getStatus());
            return execute.body();
        }
        InputStream inputStream = execute.bodyStream();
        //读取inputStream流转成file文件
        File resultFile = FileUtil.file(filePath + "token_response_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) + ".json");
        resultFile.getParentFile().mkdirs();
        FileUtil.writeFromStream(inputStream, resultFile);
        String responseContent = FileUtil.readUtf8String(resultFile);
        log.info("获取token成功");
        return responseContent;
    }
 
 
 
}