HttpClientUtils.java
4.3 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.objecteye.utils;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Component
public class HttpClientUtils {
@Value("${requestUrl}")
private String requestUrl;
@Value("${requestPath}")
private String picPath;
@Autowired
private VehicleEngine vehicleEngine;
public String VehicleAnalysis(String TPXX) {
return VehicleAnalysis("", TPXX, "", "");
}
//通过图片路径获取图片中车辆数据
public String VehicleAnalysis(String GCXH, String TPXX, String MRHPT, String HPHM) {
RestTemplate restTemplate = new RestTemplate();
// HttpHeaders headers = new HttpHeaders();
// headers.add("Content-Type", "application/json;charset=UTF-8");//标准Header
// HttpEntity<Void> httpEntity = new HttpEntity<>(headers);
Map<String, String> uriVariables = new HashMap<>();
uriVariables.put("GCXH", GCXH);
if (TPXX != null && TPXX.length() > 0) {
uriVariables.put("TPXX", TPXX);
} else {
uriVariables.put("TPXX", "/software/sy/vehicle-socket/c++/image/test.jpg");
}
uriVariables.put("MRHPT", MRHPT);
uriVariables.put("HPHM", HPHM);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(requestUrl, uriVariables, String.class);
return responseEntity.getBody();
}
//获取临时上传的图片数据信息
public String VehicleAnalysisTmp(MultipartFile TPXX) {
return VehicleAnalysisTmp("", TPXX, "", "");
}
//获取临时上传的图片数据信息
public String VehicleAnalysisTmp(String GCXH, MultipartFile TPXX, String MRHPT, String HPHM) {
String fileName = TPXX.getOriginalFilename();//name
// String tempFilePath = System.getProperty("java.io.tmpdir") + "/" + fileName;
String tempFilePath = null;
if (picPath.endsWith("/") || picPath.endsWith("\\")) {
tempFilePath = picPath + fileName;
} else {
tempFilePath = picPath + "/" + fileName;
}
File tempFile = new File(tempFilePath);
try {
TPXX.transferTo(tempFile);//生成临时文件
} catch (IOException e) {
e.printStackTrace();
}
String analysis = VehicleAnalysis(GCXH, tempFile.getAbsolutePath(), MRHPT, HPHM);
tempFile.delete();//删除临时文件文件
return analysis;
}
public String MultipartFileToBase64(MultipartFile file) throws Exception {
String base64EncoderImg = file.getOriginalFilename() + "," + Base64.encodeBase64String(file.getBytes());
String[] split = base64EncoderImg.split(",");
String split1 = split[1];
return split1.replaceAll("\r\n|\r|\n", "");
}
//将multipart文件转成车型对象
public String multiToVehicle(MultipartFile picfile) {
String vehicleInfoByBase64 = null;
try {
String base64 = MultipartFileToBase64(picfile);
vehicleInfoByBase64 = vehicleEngine.getVehicleInfoByBase64(base64);
} catch (Exception e) {
e.printStackTrace();
}
return vehicleInfoByBase64;
}
//将multipart文件转成车型对象
public String multiToHumanVehicle(MultipartFile picfile) {
String vehicleInfoByBase64 = null;
try {
String base64 = MultipartFileToBase64(picfile);
vehicleInfoByBase64 = vehicleEngine.getHumanVehicleInfoByBase64(base64);
} catch (Exception e) {
e.printStackTrace();
}
return vehicleInfoByBase64;
}
/**
* @param base64String base64码字符串
**/
public BufferedImage changeBase64StringtoImage(String base64String) {
BufferedImage buffer = null;
try {
byte[] bytes = Base64.decodeBase64(base64String);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
buffer = ImageIO.read(bais);
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
public String multiToPeople(MultipartFile picfile) {
String peopleInfoByBase64 = null;
try {
String base64 = MultipartFileToBase64(picfile);
peopleInfoByBase64 = vehicleEngine.getPeopleInfoByBase64(base64);
} catch (Exception e) {
e.printStackTrace();
}
return peopleInfoByBase64;
}
}