Blame view

src/main/java/com/objecteye/utils/HttpClientUtils.java 4.3 KB
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
1
2
  package com.objecteye.utils;
  
68a67f36   Liu Haoyu   接口问题处理;
3
  import org.apache.commons.codec.binary.Base64;
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
4
5
6
7
8
9
  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;
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  
  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) {
21d68d52   Liu Haoyu   代码重构;
29
  		return VehicleAnalysis("", TPXX, "", "");
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
30
31
32
33
34
35
36
37
38
39
40
41
42
  
  	}
  
  	//通过图片路径获取图片中车辆数据
  	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);
21d68d52   Liu Haoyu   代码重构;
43
  		if (TPXX != null && TPXX.length() > 0) {
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
44
45
46
47
48
49
50
  			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);
21d68d52   Liu Haoyu   代码重构;
51
  		return responseEntity.getBody();
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
52
53
54
55
  	}
  
  	//获取临时上传的图片数据信息
  	public String VehicleAnalysisTmp(MultipartFile TPXX) {
21d68d52   Liu Haoyu   代码重构;
56
  		return VehicleAnalysisTmp("", TPXX, "", "");
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
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
  	}
  
  	//获取临时上传的图片数据信息
  	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 {
68a67f36   Liu Haoyu   接口问题处理;
84
  		String base64EncoderImg = file.getOriginalFilename() + "," + Base64.encodeBase64String(file.getBytes());
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
85
86
  		String[] split = base64EncoderImg.split(",");
  		String split1 = split[1];
68a67f36   Liu Haoyu   接口问题处理;
87
  		return split1.replaceAll("\r\n|\r|\n", "");
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
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
  	}
  
  	//将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) {
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
118
119
  		BufferedImage buffer = null;
  		try {
46d1c007   Liu Haoyu   sun.misc.BASE64De...
120
  			byte[] bytes = Base64.decodeBase64(base64String);
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  			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;
  	}
  }