Commit fc25cfea5e5ad810cb1472234b3081e28a44e967
1 parent
c0a67eee
fix: 更新获取人员信息任务
Showing
8 changed files
with
158 additions
and
483 deletions
ruoyi-admin/pom.xml
| @@ -16,7 +16,11 @@ | @@ -16,7 +16,11 @@ | ||
| 16 | </description> | 16 | </description> |
| 17 | 17 | ||
| 18 | <dependencies> | 18 | <dependencies> |
| 19 | - | 19 | + <dependency> |
| 20 | + <groupId>cn.hutool</groupId> | ||
| 21 | + <artifactId>hutool-all</artifactId> | ||
| 22 | + <version>5.7.12</version> | ||
| 23 | + </dependency> | ||
| 20 | <!-- spring-boot-devtools --> | 24 | <!-- spring-boot-devtools --> |
| 21 | <dependency> | 25 | <dependency> |
| 22 | <groupId>org.springframework.boot</groupId> | 26 | <groupId>org.springframework.boot</groupId> |
ruoyi-admin/src/main/java/com/ruoyi/config/RestTemplateConfig.java
| @@ -3,6 +3,23 @@ package com.ruoyi.config; | @@ -3,6 +3,23 @@ package com.ruoyi.config; | ||
| 3 | import org.springframework.context.annotation.Bean; | 3 | import org.springframework.context.annotation.Bean; |
| 4 | import org.springframework.context.annotation.Configuration; | 4 | import org.springframework.context.annotation.Configuration; |
| 5 | import org.springframework.web.client.RestTemplate; | 5 | import org.springframework.web.client.RestTemplate; |
| 6 | +import org.apache.http.conn.ssl.NoopHostnameVerifier; | ||
| 7 | +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | ||
| 8 | +import org.apache.http.impl.client.CloseableHttpClient; | ||
| 9 | +import org.apache.http.impl.client.HttpClientBuilder; | ||
| 10 | +import org.apache.http.impl.client.HttpClients; | ||
| 11 | +import org.apache.http.ssl.SSLContexts; | ||
| 12 | +import org.apache.http.ssl.TrustStrategy; | ||
| 13 | +import org.springframework.http.HttpEntity; | ||
| 14 | +import org.springframework.http.HttpHeaders; | ||
| 15 | +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | ||
| 16 | +import org.springframework.stereotype.Controller; | ||
| 17 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
| 18 | +import org.springframework.web.bind.annotation.RequestMethod; | ||
| 19 | +import org.springframework.web.bind.annotation.ResponseBody; | ||
| 20 | +import org.springframework.web.client.RestTemplate; | ||
| 21 | + | ||
| 22 | +import javax.net.ssl.SSLContext; | ||
| 6 | 23 | ||
| 7 | /** | 24 | /** |
| 8 | * @author 20412 | 25 | * @author 20412 |
| @@ -11,6 +28,23 @@ import org.springframework.web.client.RestTemplate; | @@ -11,6 +28,23 @@ import org.springframework.web.client.RestTemplate; | ||
| 11 | public class RestTemplateConfig { | 28 | public class RestTemplateConfig { |
| 12 | @Bean | 29 | @Bean |
| 13 | public RestTemplate getRestTemplate(){ | 30 | public RestTemplate getRestTemplate(){ |
| 14 | - return new RestTemplate(); | 31 | + RestTemplate restTemplate = null; |
| 32 | + try { | ||
| 33 | + TrustStrategy acceptingTrustStrategy = (chain, authType) -> true; | ||
| 34 | + SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); | ||
| 35 | + SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); | ||
| 36 | + | ||
| 37 | + HttpClientBuilder clientBuilder = HttpClients.custom(); | ||
| 38 | + | ||
| 39 | + CloseableHttpClient httpClient = clientBuilder.setSSLSocketFactory(sslsf).build(); | ||
| 40 | + | ||
| 41 | + HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); | ||
| 42 | + requestFactory.setHttpClient(httpClient); | ||
| 43 | + | ||
| 44 | + restTemplate = new RestTemplate(requestFactory); | ||
| 45 | + } catch (Exception e) { | ||
| 46 | + e.printStackTrace(); | ||
| 47 | + } | ||
| 48 | + return restTemplate; | ||
| 15 | } | 49 | } |
| 16 | } | 50 | } |
ruoyi-admin/src/main/java/com/ruoyi/driver/service/impl/DriverServiceImpl.java
| @@ -7,6 +7,7 @@ import java.nio.file.Paths; | @@ -7,6 +7,7 @@ import java.nio.file.Paths; | ||
| 7 | import java.util.*; | 7 | import java.util.*; |
| 8 | import java.util.stream.Collectors; | 8 | import java.util.stream.Collectors; |
| 9 | 9 | ||
| 10 | +import cn.hutool.http.HttpUtil; | ||
| 10 | import com.ruoyi.common.config.RuoYiConfig; | 11 | import com.ruoyi.common.config.RuoYiConfig; |
| 11 | import com.ruoyi.common.core.domain.AjaxResult; | 12 | import com.ruoyi.common.core.domain.AjaxResult; |
| 12 | import com.ruoyi.common.core.redis.RedisCache; | 13 | import com.ruoyi.common.core.redis.RedisCache; |
| @@ -22,7 +23,6 @@ import com.ruoyi.pojo.request.FaceUpdateReqVo; | @@ -22,7 +23,6 @@ import com.ruoyi.pojo.request.FaceUpdateReqVo; | ||
| 22 | import com.ruoyi.pojo.response.ResponseScheduling; | 23 | import com.ruoyi.pojo.response.ResponseScheduling; |
| 23 | import com.ruoyi.service.ThreadJobService; | 24 | import com.ruoyi.service.ThreadJobService; |
| 24 | import com.ruoyi.utils.ConstDateUtil; | 25 | import com.ruoyi.utils.ConstDateUtil; |
| 25 | -import com.ruoyi.utils.HttpUtils; | ||
| 26 | import com.ruoyi.utils.ListUtils; | 26 | import com.ruoyi.utils.ListUtils; |
| 27 | import org.apache.commons.io.FilenameUtils; | 27 | import org.apache.commons.io.FilenameUtils; |
| 28 | import org.apache.tomcat.util.buf.StringUtils; | 28 | import org.apache.tomcat.util.buf.StringUtils; |
| @@ -325,7 +325,7 @@ public class DriverServiceImpl implements IDriverService { | @@ -325,7 +325,7 @@ public class DriverServiceImpl implements IDriverService { | ||
| 325 | } | 325 | } |
| 326 | log.debug("图片开始上传"); | 326 | log.debug("图片开始上传"); |
| 327 | // 获取图片数据 | 327 | // 获取图片数据 |
| 328 | - InputStream is = HttpUtils.doGet(url, new HashMap<>()); | 328 | + InputStream is = HttpUtil.createGet(url).execute().bodyStream(); |
| 329 | // 上传图片 | 329 | // 上传图片 |
| 330 | ThreadJobService.uploadImage(is, filePath); | 330 | ThreadJobService.uploadImage(is, filePath); |
| 331 | log.debug("图片上传完毕"); | 331 | log.debug("图片上传完毕"); |
ruoyi-admin/src/main/java/com/ruoyi/in/service/impl/SignInServiceImpl.java
| @@ -126,8 +126,10 @@ public class SignInServiceImpl implements ISignInService { | @@ -126,8 +126,10 @@ public class SignInServiceImpl implements ISignInService { | ||
| 126 | return result; | 126 | return result; |
| 127 | } | 127 | } |
| 128 | } | 128 | } |
| 129 | - | ||
| 130 | - return SIGN_IN_SUCCESS.equals(signIn.getStatus()) ? AjaxResult.success(SIGN_IN_SUCCESS_STRING, vo) : AjaxResult.error(SIGN_IN_ERROR + signIn.getRemark(), vo); | 129 | + if (SIGN_IN_FAIL.equals(signIn.getStatus())){ |
| 130 | + return AjaxResult.error(SIGN_IN_ERROR + signIn.getRemark(), vo); | ||
| 131 | + } | ||
| 132 | + return AjaxResult.success(SIGN_IN_SUCCESS_STRING, vo); | ||
| 131 | } | 133 | } |
| 132 | 134 | ||
| 133 | /** | 135 | /** |
| @@ -218,7 +220,7 @@ public class SignInServiceImpl implements ISignInService { | @@ -218,7 +220,7 @@ public class SignInServiceImpl implements ISignInService { | ||
| 218 | count = redisCache.setIncrementMapValue(key, signIn.getJobCode(), 1); | 220 | count = redisCache.setIncrementMapValue(key, signIn.getJobCode(), 1); |
| 219 | } | 221 | } |
| 220 | if (SIGN_IN_ERROR_COUNT.compareTo(count) <= 0) { | 222 | if (SIGN_IN_ERROR_COUNT.compareTo(count) <= 0) { |
| 221 | - return AjaxResult.warn(SIGN_IN_ERROR + signIn.getRemark() + "酒精测试不通过" + count + "次请更换车辆驾驶员", vo); | 223 | + return AjaxResult.warn(SIGN_IN_ERROR + ": "+ signIn.getRemark() + "酒精测试不通过" + count + "次请更换车辆驾驶员。", vo); |
| 222 | } | 224 | } |
| 223 | return null; | 225 | return null; |
| 224 | } | 226 | } |
ruoyi-admin/src/main/java/com/ruoyi/job/DriverJob.java
| 1 | package com.ruoyi.job; | 1 | package com.ruoyi.job; |
| 2 | 2 | ||
| 3 | +import cn.hutool.http.HttpUtil; | ||
| 3 | import com.alibaba.fastjson2.JSON; | 4 | import com.alibaba.fastjson2.JSON; |
| 4 | import com.alibaba.fastjson2.JSONArray; | 5 | import com.alibaba.fastjson2.JSONArray; |
| 5 | import com.ruoyi.common.core.redis.RedisCache; | 6 | import com.ruoyi.common.core.redis.RedisCache; |
| @@ -11,8 +12,6 @@ import com.ruoyi.pojo.response.ResponseScheduling; | @@ -11,8 +12,6 @@ import com.ruoyi.pojo.response.ResponseScheduling; | ||
| 11 | import com.ruoyi.pojo.response.personnel.*; | 12 | import com.ruoyi.pojo.response.personnel.*; |
| 12 | import com.ruoyi.service.ThreadJobService; | 13 | import com.ruoyi.service.ThreadJobService; |
| 13 | import com.ruoyi.utils.ConstDateUtil; | 14 | import com.ruoyi.utils.ConstDateUtil; |
| 14 | -import com.ruoyi.utils.HttpUtils; | ||
| 15 | -import com.ruoyi.utils.ListUtils; | ||
| 16 | import lombok.extern.slf4j.Slf4j; | 15 | import lombok.extern.slf4j.Slf4j; |
| 17 | import org.springframework.beans.factory.InitializingBean; | 16 | import org.springframework.beans.factory.InitializingBean; |
| 18 | import org.springframework.beans.factory.annotation.Autowired; | 17 | import org.springframework.beans.factory.annotation.Autowired; |
| @@ -101,9 +100,7 @@ public class DriverJob implements InitializingBean { | @@ -101,9 +100,7 @@ public class DriverJob implements InitializingBean { | ||
| 101 | //获取token | 100 | //获取token |
| 102 | TokenResponseVo tokenVo = getToken(TOKEN_URL); | 101 | TokenResponseVo tokenVo = getToken(TOKEN_URL); |
| 103 | // 获取驾驶员信息 | 102 | // 获取驾驶员信息 |
| 104 | - List<Driver> drivers = getDrivers(tokenVo.getAccessToken()); | ||
| 105 | - // 插入信息 | ||
| 106 | - saveDrivers(drivers, tokenVo.getAccessToken()); | 103 | + getDrivers(tokenVo.getAccessToken()); |
| 107 | 104 | ||
| 108 | } catch (Exception e) { | 105 | } catch (Exception e) { |
| 109 | log.info("执行失败:" + e.getMessage()); | 106 | log.info("执行失败:" + e.getMessage()); |
| @@ -111,14 +108,10 @@ public class DriverJob implements InitializingBean { | @@ -111,14 +108,10 @@ public class DriverJob implements InitializingBean { | ||
| 111 | log.info("执行结束"); | 108 | log.info("执行结束"); |
| 112 | } | 109 | } |
| 113 | 110 | ||
| 114 | - private void saveDrivers(List<Driver> drivers, String accessToken) { | ||
| 115 | - // 获取图片 | ||
| 116 | - List<List<Driver>> list = ListUtils.splitList(drivers, 800); | ||
| 117 | - for (List<Driver> driverList : list) { | ||
| 118 | - // 多线程插入数据 | ||
| 119 | - THREAD_JOB_SERVICE.asyncUploadDriverWithUpdateImageUrl(driverList, accessToken); | ||
| 120 | - log.info("开始插入"); | ||
| 121 | - } | 111 | + private static void saveDrivers(List<Driver> drivers, String accessToken) { |
| 112 | + // 多线程插入数据 | ||
| 113 | + log.info("开始插入"); | ||
| 114 | + THREAD_JOB_SERVICE.asyncUploadDriverWithUpdateImageUrl(drivers, accessToken); | ||
| 122 | // String downloadImage = getDownloadImage(url, accessToken, ""); | 115 | // String downloadImage = getDownloadImage(url, accessToken, ""); |
| 123 | 116 | ||
| 124 | } | 117 | } |
| @@ -139,6 +132,7 @@ public class DriverJob implements InitializingBean { | @@ -139,6 +132,7 @@ public class DriverJob implements InitializingBean { | ||
| 139 | } catch (UnsupportedEncodingException e) { | 132 | } catch (UnsupportedEncodingException e) { |
| 140 | throw new RuntimeException(e); | 133 | throw new RuntimeException(e); |
| 141 | } | 134 | } |
| 135 | + url = url + "?userId=InterfaceManagement&timeout=7200000&fileUrl=" + fileUrl + "&systemToken=16A66291CHE9K5DPE1IDO9E63FOE2VWA09QFLV"; | ||
| 142 | Map<String, Object> param = new HashMap<>(); | 136 | Map<String, Object> param = new HashMap<>(); |
| 143 | param.put("userId", "InterfaceManagement"); | 137 | param.put("userId", "InterfaceManagement"); |
| 144 | param.put("timeout", 7200000L); | 138 | param.put("timeout", 7200000L); |
| @@ -148,17 +142,15 @@ public class DriverJob implements InitializingBean { | @@ -148,17 +142,15 @@ public class DriverJob implements InitializingBean { | ||
| 148 | header.put("x-acs-dingtalk-access-token", accessToken); | 142 | header.put("x-acs-dingtalk-access-token", accessToken); |
| 149 | header.put("Content-Type", "application/json"); | 143 | header.put("Content-Type", "application/json"); |
| 150 | String result = ""; | 144 | String result = ""; |
| 151 | - | ||
| 152 | try { | 145 | try { |
| 153 | - String s = HttpUtils.doGet(url, param, header); | ||
| 154 | - ImageUrlResultResponseVo vo = JSON.parseObject(s, ImageUrlResultResponseVo.class); | ||
| 155 | - result = vo.getResult(); | 146 | + String body = HttpUtil.createGet(url).addHeaders(header).form(new HashMap<>()).execute().body(); |
| 147 | + result = JSON.parseObject(body, ImageUrlResultResponseVo.class).getResult(); | ||
| 156 | // 可能需要重新获取token | 148 | // 可能需要重新获取token |
| 157 | } catch (Exception e) { | 149 | } catch (Exception e) { |
| 158 | String token = DriverJob.getToken(TOKEN_URL).getAccessToken(); | 150 | String token = DriverJob.getToken(TOKEN_URL).getAccessToken(); |
| 159 | header.put("x-acs-dingtalk-access-token", token); | 151 | header.put("x-acs-dingtalk-access-token", token); |
| 160 | - String s = HttpUtils.doGet(url, param, header); | ||
| 161 | - ImageUrlResultResponseVo vo = JSON.parseObject(s, ImageUrlResultResponseVo.class); | 152 | + String body = HttpUtil.createGet(url).addHeaders(header).form(new HashMap<>()).execute().body(); |
| 153 | + ImageUrlResultResponseVo vo = JSON.parseObject(body, ImageUrlResultResponseVo.class); | ||
| 162 | result = vo.getResult(); | 154 | result = vo.getResult(); |
| 163 | } | 155 | } |
| 164 | return result; | 156 | return result; |
| @@ -223,35 +215,53 @@ public class DriverJob implements InitializingBean { | @@ -223,35 +215,53 @@ public class DriverJob implements InitializingBean { | ||
| 223 | return driverSchedulingMap; | 215 | return driverSchedulingMap; |
| 224 | } | 216 | } |
| 225 | 217 | ||
| 226 | - public static List<Driver> getDrivers(String accessToken) throws Exception { | 218 | + public static void getDrivers(String accessToken) throws Exception { |
| 227 | // Map<String, String> configMap = getStringStringMap(timestamp); | 219 | // Map<String, String> configMap = getStringStringMap(timestamp); |
| 228 | // String sign = getSHA1(configMap); | 220 | // String sign = getSHA1(configMap); |
| 229 | Date date = new Date(); | 221 | Date date = new Date(); |
| 230 | - PersonnelResultResponseVo vo = getPersonInfo(accessToken); | ||
| 231 | - return vo.getData().stream().map(item -> { | 222 | + int pageSize = 100; |
| 223 | + PersonnelResultResponseVo vo = getPersonInfo(accessToken, pageSize, 1); | ||
| 224 | + int countPage = vo.getTotalCount() / pageSize; | ||
| 225 | + countPage = vo.getTotalCount() % pageSize == 0 ? countPage : countPage + 1; | ||
| 226 | + List<Driver> drivers = getDrivers(date, vo); | ||
| 227 | + saveDrivers(drivers, accessToken); | ||
| 228 | + for (int i = 1; i <= countPage; ) { | ||
| 229 | + if (++i <= countPage) { | ||
| 230 | + vo = getPersonInfo(accessToken, 100, i); | ||
| 231 | + drivers = getDrivers(date, vo); | ||
| 232 | + saveDrivers(drivers, accessToken); | ||
| 233 | + } | ||
| 234 | + } | ||
| 235 | + | ||
| 236 | +// return drivers; | ||
| 237 | + } | ||
| 238 | + | ||
| 239 | + private static List<Driver> getDrivers(Date date, PersonnelResultResponseVo vo) { | ||
| 240 | + List<Driver> drivers = vo.getData().stream().map(item -> { | ||
| 232 | Driver driver = new Driver(); | 241 | Driver driver = new Driver(); |
| 233 | FormData formData = item.getFormData(); | 242 | FormData formData = item.getFormData(); |
| 234 | driver.setUpdateTime(date); | 243 | driver.setUpdateTime(date); |
| 235 | driver.setJobCode(formData.getTextField_lk9mk222()); | 244 | driver.setJobCode(formData.getTextField_lk9mk222()); |
| 236 | driver.setPersonnelName(formData.getTextField_lk9mk224()); | 245 | driver.setPersonnelName(formData.getTextField_lk9mk224()); |
| 237 | driver.setPosts(formData.getTextField_lk9mk226()); | 246 | driver.setPosts(formData.getTextField_lk9mk226()); |
| 238 | -// driver.setImage(form); | ||
| 239 | // 解析JSON字符串 | 247 | // 解析JSON字符串 |
| 240 | List<ImageField_lk9mk228> lists = JSONArray.parseArray(formData.getImageField_lk9mk228(), ImageField_lk9mk228.class); | 248 | List<ImageField_lk9mk228> lists = JSONArray.parseArray(formData.getImageField_lk9mk228(), ImageField_lk9mk228.class); |
| 241 | driver.setImage(lists.get(0).getPreviewUrl()); | 249 | driver.setImage(lists.get(0).getPreviewUrl()); |
| 242 | return driver; | 250 | return driver; |
| 243 | }).collect(Collectors.toList()); | 251 | }).collect(Collectors.toList()); |
| 244 | -// return drivers; | 252 | + return drivers; |
| 245 | } | 253 | } |
| 246 | 254 | ||
| 247 | - private static PersonnelResultResponseVo getPersonInfo(String accessToken) { | 255 | + private static PersonnelResultResponseVo getPersonInfo(String accessToken, Integer pageSize, Integer currentPage) { |
| 248 | RestTemplate restTemplate = new RestTemplate(); | 256 | RestTemplate restTemplate = new RestTemplate(); |
| 249 | String url = "https://api.dingtalk.com/v1.0/yida/forms/instances/search"; | 257 | String url = "https://api.dingtalk.com/v1.0/yida/forms/instances/search"; |
| 250 | PersonnelRequestVo vo = new PersonnelRequestVo(); | 258 | PersonnelRequestVo vo = new PersonnelRequestVo(); |
| 251 | vo.setAppType("APP_HV8J7X8PFRXLJJW8JTZK"); | 259 | vo.setAppType("APP_HV8J7X8PFRXLJJW8JTZK"); |
| 252 | - vo.setFormUuid("FORM-CP766081XRMCZBZC7URI45AVVB662XFNMKAKLB"); | 260 | + vo.setFormUuid("FORM-D2B665D1LQMCRRGS9WE6F54QTVYF25BXHM9KL4"); |
| 253 | vo.setUserId("InterfaceManagement"); | 261 | vo.setUserId("InterfaceManagement"); |
| 254 | vo.setSystemToken("16A66291CHE9K5DPE1IDO9E63FOE2VWA09QFLV"); | 262 | vo.setSystemToken("16A66291CHE9K5DPE1IDO9E63FOE2VWA09QFLV"); |
| 263 | + vo.setCurrentPage(currentPage); | ||
| 264 | + vo.setPageSize(pageSize); | ||
| 255 | // 工号组件 | 265 | // 工号组件 |
| 256 | // vo.setSearchFieldJson(" size = 4"); | 266 | // vo.setSearchFieldJson(" size = 4"); |
| 257 | // 设置请求头 | 267 | // 设置请求头 |
ruoyi-admin/src/main/java/com/ruoyi/pojo/request/PersonnelRequestVo.java
| @@ -12,4 +12,12 @@ public class PersonnelRequestVo { | @@ -12,4 +12,12 @@ public class PersonnelRequestVo { | ||
| 12 | * 非必须 | 12 | * 非必须 |
| 13 | */ | 13 | */ |
| 14 | String searchFieldJson; | 14 | String searchFieldJson; |
| 15 | + /** | ||
| 16 | + * 非必须 | ||
| 17 | + */ | ||
| 18 | + Integer currentPage; | ||
| 19 | + /** | ||
| 20 | + * 非必须 | ||
| 21 | + */ | ||
| 22 | + Integer pageSize; | ||
| 15 | } | 23 | } |
ruoyi-admin/src/main/java/com/ruoyi/service/ThreadJobService.java
| 1 | package com.ruoyi.service; | 1 | package com.ruoyi.service; |
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | +import cn.hutool.http.HttpUtil; | ||
| 4 | import com.ruoyi.common.config.RuoYiConfig; | 5 | import com.ruoyi.common.config.RuoYiConfig; |
| 5 | -import com.ruoyi.common.constant.Constants; | ||
| 6 | import com.ruoyi.common.exception.file.FileUploadException; | 6 | import com.ruoyi.common.exception.file.FileUploadException; |
| 7 | -import com.ruoyi.common.utils.DateUtils; | ||
| 8 | import com.ruoyi.common.utils.StringUtils; | 7 | import com.ruoyi.common.utils.StringUtils; |
| 9 | import com.ruoyi.common.utils.file.FileUploadUtils; | 8 | import com.ruoyi.common.utils.file.FileUploadUtils; |
| 10 | -import com.ruoyi.common.utils.file.FileUtils; | ||
| 11 | -import com.ruoyi.common.utils.uuid.Seq; | ||
| 12 | -import com.ruoyi.common.utils.uuid.UUID; | ||
| 13 | import com.ruoyi.driver.domain.Driver; | 9 | import com.ruoyi.driver.domain.Driver; |
| 14 | import com.ruoyi.driver.mapper.DriverMapper; | 10 | import com.ruoyi.driver.mapper.DriverMapper; |
| 15 | import com.ruoyi.job.DriverJob; | 11 | import com.ruoyi.job.DriverJob; |
| 16 | -import com.ruoyi.utils.HttpUtils; | ||
| 17 | import lombok.extern.slf4j.Slf4j; | 12 | import lombok.extern.slf4j.Slf4j; |
| 18 | -import org.apache.commons.io.FilenameUtils; | ||
| 19 | import org.springframework.beans.factory.annotation.Autowired; | 13 | import org.springframework.beans.factory.annotation.Autowired; |
| 20 | import org.springframework.beans.factory.annotation.Value; | 14 | import org.springframework.beans.factory.annotation.Value; |
| 21 | import org.springframework.scheduling.annotation.Async; | 15 | import org.springframework.scheduling.annotation.Async; |
| 22 | import org.springframework.scheduling.annotation.EnableAsync; | 16 | import org.springframework.scheduling.annotation.EnableAsync; |
| 23 | import org.springframework.stereotype.Component; | 17 | import org.springframework.stereotype.Component; |
| 18 | +import org.springframework.transaction.PlatformTransactionManager; | ||
| 19 | +import org.springframework.transaction.TransactionStatus; | ||
| 20 | +import org.springframework.transaction.annotation.Transactional; | ||
| 21 | +import org.springframework.transaction.support.DefaultTransactionDefinition; | ||
| 24 | import org.springframework.web.client.RestTemplate; | 22 | import org.springframework.web.client.RestTemplate; |
| 25 | import sun.misc.BASE64Decoder; | 23 | import sun.misc.BASE64Decoder; |
| 26 | 24 | ||
| 27 | import java.io.*; | 25 | import java.io.*; |
| 28 | -import java.nio.file.Files; | ||
| 29 | -import java.nio.file.Paths; | ||
| 30 | -import java.util.Base64; | ||
| 31 | -import java.util.HashMap; | ||
| 32 | import java.util.List; | 26 | import java.util.List; |
| 33 | import java.util.Objects; | 27 | import java.util.Objects; |
| 34 | 28 | ||
| @@ -46,9 +40,15 @@ public class ThreadJobService { | @@ -46,9 +40,15 @@ public class ThreadJobService { | ||
| 46 | @Autowired | 40 | @Autowired |
| 47 | private DriverMapper driverMapper; | 41 | private DriverMapper driverMapper; |
| 48 | 42 | ||
| 43 | + @Autowired | ||
| 44 | + private PlatformTransactionManager transactionManager; | ||
| 45 | + | ||
| 49 | @Value("${api.headImage}") | 46 | @Value("${api.headImage}") |
| 50 | private String headImagePre; | 47 | private String headImagePre; |
| 51 | 48 | ||
| 49 | + @Autowired | ||
| 50 | + private RestTemplate restTemplate; | ||
| 51 | + | ||
| 52 | 52 | ||
| 53 | /** | 53 | /** |
| 54 | * 异步上传图片 | 54 | * 异步上传图片 |
| @@ -104,46 +104,75 @@ public class ThreadJobService { | @@ -104,46 +104,75 @@ public class ThreadJobService { | ||
| 104 | public void asyncUploadDriverWithUpdateImageUrl(List<Driver> drivers, String token) { | 104 | public void asyncUploadDriverWithUpdateImageUrl(List<Driver> drivers, String token) { |
| 105 | // 插入数据 | 105 | // 插入数据 |
| 106 | for (Driver driver : drivers) { | 106 | for (Driver driver : drivers) { |
| 107 | - String imageUlr = driver.getImage(); | ||
| 108 | - // 生成文件路径 | ||
| 109 | - String fileName = driver.getJobCode() + ".png"; | ||
| 110 | - String filePath = RuoYiConfig.getUploadPath() + headImagePre + "/" + fileName; | 107 | + // 开启事务 |
| 108 | + TransactionStatus transaction = transactionManager.getTransaction(new DefaultTransactionDefinition()); | ||
| 111 | try { | 109 | try { |
| 112 | - fileName = FileUploadUtils.getPathFileName(RuoYiConfig.getUploadPath() + headImagePre, fileName); | ||
| 113 | - } catch (IOException e) { | ||
| 114 | - throw new RuntimeException(e); | 110 | + // 插入数据 |
| 111 | + insertDriverInfo(token, driver); | ||
| 112 | + // 提交事务 | ||
| 113 | + transactionManager.commit(transaction); | ||
| 114 | + } catch (Exception e) { | ||
| 115 | + // 回滚事务 | ||
| 116 | + transactionManager.rollback(transaction); | ||
| 117 | + log.error("保存数据是出现了异常:{}",e.getMessage()); | ||
| 115 | } | 118 | } |
| 116 | - driver.setImage(fileName); | ||
| 117 | - // 插入数据 如果员工已经存在在不在下载图片 | ||
| 118 | - int result = driverMapper.insertDriver(driver); | ||
| 119 | - log.debug("插入完毕"); | ||
| 120 | - if (result == 0) { | ||
| 121 | - continue; | ||
| 122 | - } | ||
| 123 | - log.info("图片开始上传"); | ||
| 124 | - // 获取图片请求地址 | ||
| 125 | - String imageUrl = DriverJob.getDownloadImageUrl(token, imageUlr); | ||
| 126 | - // 获取图片数据 | ||
| 127 | - InputStream is = HttpUtils.doGet(imageUrl, new HashMap<>()); | ||
| 128 | - // 上传图片 | 119 | + } |
| 120 | + | ||
| 121 | + | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + private void insertDriverInfo(String token, Driver driver) { | ||
| 125 | + String headImageUrl = new String(driver.getImage()); | ||
| 126 | + // 生成文件路径 | ||
| 127 | + String fileName = driver.getJobCode() + ".png"; | ||
| 128 | + String filePath = RuoYiConfig.getUploadPath() + headImagePre + "/" + fileName; | ||
| 129 | + try { | ||
| 130 | + fileName = FileUploadUtils.getPathFileName(RuoYiConfig.getUploadPath() + headImagePre, fileName); | ||
| 131 | + } catch (IOException e) { | ||
| 132 | + throw new RuntimeException(e); | ||
| 133 | + } | ||
| 134 | + driver.setImage(fileName); | ||
| 135 | + // 插入数据 如果员工已经存在在不在下载图片 | ||
| 136 | + String imageUrl = DriverJob.getDownloadImageUrl(token, headImageUrl); | ||
| 137 | + // 获取图片请求地址 | ||
| 138 | + if (StringUtils.isEmpty(imageUrl)) { | ||
| 139 | + log.error("工号:{},图片缺失,放弃保存员工信息,等待员工图片手动上传。", driver.getJobCode()); | ||
| 140 | + return; | ||
| 141 | + } | ||
| 142 | + int result = driverMapper.insertDriver(driver); | ||
| 143 | + log.debug("插入完毕"); | ||
| 144 | + if (result == 0) { | ||
| 145 | + return; | ||
| 146 | + } | ||
| 147 | + log.info("图片开始上传"); | ||
| 148 | + // 获取图片数据 | ||
| 149 | + InputStream is = getImageInputStreamByUrl(imageUrl); | ||
| 150 | + // 上传图片 | ||
| 151 | + try { | ||
| 152 | + uploadImage(is, filePath); | ||
| 153 | + } catch (IOException e) { | ||
| 154 | + log.error("工号:{}的人脸图像,上传失败:{}" , driver.getJobCode(), e.getMessage()); | ||
| 155 | + is = getImageInputStreamByUrl(imageUrl); | ||
| 129 | try { | 156 | try { |
| 130 | uploadImage(is, filePath); | 157 | uploadImage(is, filePath); |
| 131 | - } catch (IOException e) { | ||
| 132 | - log.error(e.getMessage()); | ||
| 133 | - throw new RuntimeException(e); | 158 | + } catch (IOException ex) { |
| 159 | + log.error("工号:{}的人脸图像,再次上传失败:{}" , driver.getJobCode(), e.getMessage()); | ||
| 160 | + throw new RuntimeException(ex); | ||
| 134 | } | 161 | } |
| 135 | - log.debug("图片上传完毕"); | ||
| 136 | } | 162 | } |
| 163 | + log.info("图片上传完毕"); | ||
| 164 | + } | ||
| 137 | 165 | ||
| 138 | - | 166 | + private InputStream getImageInputStreamByUrl(String imageUrl) { |
| 167 | + return HttpUtil.createGet(imageUrl).execute().bodyStream(); | ||
| 139 | } | 168 | } |
| 140 | 169 | ||
| 141 | public static void uploadImage(InputStream is, String filePath) throws IOException { | 170 | public static void uploadImage(InputStream is, String filePath) throws IOException { |
| 142 | - if (Objects.isNull(is)){ | 171 | + if (Objects.isNull(is)) { |
| 143 | throw new IOException("图片数据不存在"); | 172 | throw new IOException("图片数据不存在"); |
| 144 | } | 173 | } |
| 145 | File file = new File(filePath + File.separator); | 174 | File file = new File(filePath + File.separator); |
| 146 | - log.info("文件路径:{}",file.getPath()); | 175 | + log.info("文件路径:{}", file.getPath()); |
| 147 | if (!file.exists()) { | 176 | if (!file.exists()) { |
| 148 | if (!file.getParentFile().exists()) { | 177 | if (!file.getParentFile().exists()) { |
| 149 | file.getParentFile().mkdirs(); | 178 | file.getParentFile().mkdirs(); |
ruoyi-admin/src/main/java/com/ruoyi/utils/HttpUtils.java deleted
100644 → 0
| 1 | -package com.ruoyi.utils; | ||
| 2 | - | ||
| 3 | -import lombok.extern.slf4j.Slf4j; | ||
| 4 | -import org.apache.commons.collections.CollectionUtils; | ||
| 5 | -import org.apache.commons.io.IOUtils; | ||
| 6 | -import org.apache.http.HttpEntity; | ||
| 7 | -import org.apache.http.HttpResponse; | ||
| 8 | -import org.apache.http.NameValuePair; | ||
| 9 | -import org.apache.http.client.config.RequestConfig; | ||
| 10 | -import org.apache.http.client.entity.UrlEncodedFormEntity; | ||
| 11 | -import org.apache.http.client.methods.CloseableHttpResponse; | ||
| 12 | -import org.apache.http.client.methods.HttpGet; | ||
| 13 | -import org.apache.http.client.methods.HttpPost; | ||
| 14 | -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | ||
| 15 | -import org.apache.http.conn.ssl.TrustStrategy; | ||
| 16 | -import org.apache.http.entity.StringEntity; | ||
| 17 | -import org.apache.http.impl.client.CloseableHttpClient; | ||
| 18 | -import org.apache.http.impl.client.HttpClients; | ||
| 19 | -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | ||
| 20 | -import org.apache.http.message.BasicNameValuePair; | ||
| 21 | -import org.apache.http.ssl.SSLContextBuilder; | ||
| 22 | -import org.apache.http.util.EntityUtils; | ||
| 23 | -import org.slf4j.Logger; | ||
| 24 | -import org.slf4j.LoggerFactory; | ||
| 25 | - | ||
| 26 | -import javax.net.ssl.HostnameVerifier; | ||
| 27 | -import javax.net.ssl.SSLContext; | ||
| 28 | -import javax.net.ssl.SSLSession; | ||
| 29 | -import java.io.IOException; | ||
| 30 | -import java.io.InputStream; | ||
| 31 | -import java.security.GeneralSecurityException; | ||
| 32 | -import java.security.cert.CertificateException; | ||
| 33 | -import java.security.cert.X509Certificate; | ||
| 34 | -import java.util.*; | ||
| 35 | - | ||
| 36 | -@Slf4j | ||
| 37 | -public class HttpUtils { | ||
| 38 | - private static PoolingHttpClientConnectionManager connMgr; | ||
| 39 | - private static RequestConfig requestConfig; | ||
| 40 | - private static final int MAX_TIMEOUT = 10000; | ||
| 41 | - | ||
| 42 | - private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class); | ||
| 43 | - | ||
| 44 | - static { | ||
| 45 | - // 设置连接池 | ||
| 46 | - connMgr = new PoolingHttpClientConnectionManager(); | ||
| 47 | - // 设置连接池大小 | ||
| 48 | - connMgr.setMaxTotal(20); | ||
| 49 | - connMgr.setDefaultMaxPerRoute(connMgr.getMaxTotal()); | ||
| 50 | - // Validate connections after 1 sec of inactivity | ||
| 51 | - connMgr.setValidateAfterInactivity(5000); | ||
| 52 | - RequestConfig.Builder configBuilder = RequestConfig.custom(); | ||
| 53 | - // 设置连接超时 | ||
| 54 | - configBuilder.setConnectTimeout(MAX_TIMEOUT); | ||
| 55 | - // 设置读取超时 | ||
| 56 | - configBuilder.setSocketTimeout(MAX_TIMEOUT); | ||
| 57 | - // 设置从连接池获取连接实例的超时 | ||
| 58 | - configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT); | ||
| 59 | - | ||
| 60 | - requestConfig = configBuilder.build(); | ||
| 61 | - } | ||
| 62 | - | ||
| 63 | - /** | ||
| 64 | - * 发送 GET 请求(HTTP),不带输入数据 | ||
| 65 | - * | ||
| 66 | - * @param url | ||
| 67 | - * @return | ||
| 68 | - */ | ||
| 69 | - public static String doGet(String url) { | ||
| 70 | - return doGet(url, new HashMap<String, Object>(), new HashMap<>()); | ||
| 71 | - } | ||
| 72 | - | ||
| 73 | - /** | ||
| 74 | - * 发送 GET 请求(HTTP),K-V形式 | ||
| 75 | - * | ||
| 76 | - * @param url | ||
| 77 | - * @param params | ||
| 78 | - * @return | ||
| 79 | - */ | ||
| 80 | - public static String doGet(String url, Map<String, Object> params, Map<String, String> headers) { | ||
| 81 | - String apiUrl = url; | ||
| 82 | - StringBuffer param = new StringBuffer(); | ||
| 83 | - int i = 0; | ||
| 84 | - for (String key : params.keySet()) { | ||
| 85 | - if (i == 0) | ||
| 86 | - param.append("?"); | ||
| 87 | - else | ||
| 88 | - param.append("&"); | ||
| 89 | - param.append(key).append("=").append(params.get(key)); | ||
| 90 | - i++; | ||
| 91 | - } | ||
| 92 | - apiUrl += param; | ||
| 93 | - String result = null; | ||
| 94 | - CloseableHttpClient httpClient = null; | ||
| 95 | - if (apiUrl.startsWith("https")) { | ||
| 96 | - httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()) | ||
| 97 | - .setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build(); | ||
| 98 | - } else { | ||
| 99 | - httpClient = HttpClients.createDefault(); | ||
| 100 | - } | ||
| 101 | - HttpResponse response = null; | ||
| 102 | - try { | ||
| 103 | - log.info("HttpUrl:{}",apiUrl); | ||
| 104 | - HttpGet httpGet = new HttpGet(apiUrl); | ||
| 105 | - httpGet.setConfig(requestConfig); | ||
| 106 | - for (Map.Entry<String, String> header : headers.entrySet()) { | ||
| 107 | - httpGet.addHeader(header.getKey(), header.getValue()); | ||
| 108 | - } | ||
| 109 | - response = httpClient.execute(httpGet); | ||
| 110 | - HttpEntity entity = response.getEntity(); | ||
| 111 | - if (entity != null) { | ||
| 112 | - InputStream instream = entity.getContent(); | ||
| 113 | - result = IOUtils.toString(instream, "UTF-8"); | ||
| 114 | - logger.info("Http Result:{}",result); | ||
| 115 | - } | ||
| 116 | - } catch (IOException e) { | ||
| 117 | - logger.error("Http Error:{}",e.getMessage()); | ||
| 118 | - try { | ||
| 119 | - if (httpClient != null) { | ||
| 120 | - httpClient.close(); | ||
| 121 | - } | ||
| 122 | - if (response != null) { | ||
| 123 | - EntityUtils.consume(response.getEntity()); | ||
| 124 | - } | ||
| 125 | - } catch (IOException exception) { | ||
| 126 | - exception.printStackTrace(); | ||
| 127 | - } | ||
| 128 | - } | ||
| 129 | - return result; | ||
| 130 | - } | ||
| 131 | - | ||
| 132 | - public static InputStream doGet(String url,Map<String, Object> params) { | ||
| 133 | - HashMap<String, String> headers = new HashMap<>(); | ||
| 134 | - String apiUrl = url; | ||
| 135 | - StringBuffer param = new StringBuffer(); | ||
| 136 | - int i = 0; | ||
| 137 | - for (String key : params.keySet()) { | ||
| 138 | - if (i == 0) | ||
| 139 | - param.append("?"); | ||
| 140 | - else | ||
| 141 | - param.append("&"); | ||
| 142 | - param.append(key).append("=").append(params.get(key)); | ||
| 143 | - i++; | ||
| 144 | - } | ||
| 145 | - apiUrl += param; | ||
| 146 | - InputStream result = null; | ||
| 147 | - CloseableHttpClient httpClient = null; | ||
| 148 | - if (apiUrl.startsWith("https")) { | ||
| 149 | - httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()) | ||
| 150 | - .setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build(); | ||
| 151 | - } else { | ||
| 152 | - httpClient = HttpClients.createDefault(); | ||
| 153 | - } | ||
| 154 | - HttpResponse response = null; | ||
| 155 | - try { | ||
| 156 | - log.info("HttpUrl:{}",apiUrl); | ||
| 157 | - HttpGet httpGet = new HttpGet(apiUrl); | ||
| 158 | - httpGet.setConfig(requestConfig); | ||
| 159 | - for (Map.Entry<String, String> header : headers.entrySet()) { | ||
| 160 | - httpGet.addHeader(header.getKey(), header.getValue()); | ||
| 161 | - } | ||
| 162 | - response = httpClient.execute(httpGet); | ||
| 163 | - HttpEntity entity = response.getEntity(); | ||
| 164 | - if (entity != null) { | ||
| 165 | - result = entity.getContent(); | ||
| 166 | - logger.info("Http Result:{}",result); | ||
| 167 | - } | ||
| 168 | - } catch (IOException e) { | ||
| 169 | - logger.error("Http Error:{}",e.getMessage()); | ||
| 170 | - try { | ||
| 171 | - if (httpClient != null) { | ||
| 172 | - httpClient.close(); | ||
| 173 | - } | ||
| 174 | - if (response != null) { | ||
| 175 | - EntityUtils.consume(response.getEntity()); | ||
| 176 | - } | ||
| 177 | - } catch (IOException exception) { | ||
| 178 | - exception.printStackTrace(); | ||
| 179 | - } | ||
| 180 | - } | ||
| 181 | - return result; | ||
| 182 | - } | ||
| 183 | - /** | ||
| 184 | - * 发送 GET 请求(HTTP),K-V形式--------取消重定向!!!!!! | ||
| 185 | - * | ||
| 186 | - * @param url | ||
| 187 | - * @param params | ||
| 188 | - * @return | ||
| 189 | - */ | ||
| 190 | - public static HttpResponse doGetAndReturnResponse(String url, Map<String, Object> params, Map<String, String> headers) { | ||
| 191 | - String apiUrl = url; | ||
| 192 | - StringBuffer param = new StringBuffer(); | ||
| 193 | - int i = 0; | ||
| 194 | - for (String key : params.keySet()) { | ||
| 195 | - if (i == 0) | ||
| 196 | - param.append("?"); | ||
| 197 | - else | ||
| 198 | - param.append("&"); | ||
| 199 | - param.append(key).append("=").append(params.get(key)); | ||
| 200 | - i++; | ||
| 201 | - } | ||
| 202 | - apiUrl += param; | ||
| 203 | - | ||
| 204 | - CloseableHttpClient httpClient = null; | ||
| 205 | - if (apiUrl.startsWith("https")) { | ||
| 206 | - httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()) | ||
| 207 | - .setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build(); | ||
| 208 | - } else { | ||
| 209 | - httpClient = HttpClients.createDefault(); | ||
| 210 | - } | ||
| 211 | - CloseableHttpResponse response = null; | ||
| 212 | - try { | ||
| 213 | - HttpGet httpGet = new HttpGet(apiUrl); | ||
| 214 | - httpGet.setConfig(requestConfig); | ||
| 215 | - | ||
| 216 | - //取消强制重定向!!!!! | ||
| 217 | - httpGet.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build()); | ||
| 218 | - | ||
| 219 | - | ||
| 220 | - for (Map.Entry<String, String> header : headers.entrySet()) { | ||
| 221 | - httpGet.addHeader(header.getKey(), header.getValue()); | ||
| 222 | - } | ||
| 223 | - response = httpClient.execute(httpGet); | ||
| 224 | - | ||
| 225 | - return response; | ||
| 226 | - } catch (IOException e) { | ||
| 227 | - try { | ||
| 228 | - if (httpClient != null) { | ||
| 229 | - httpClient.close(); | ||
| 230 | - } | ||
| 231 | - if (response != null) { | ||
| 232 | - EntityUtils.consume(response.getEntity()); | ||
| 233 | - } | ||
| 234 | - } catch (IOException exception) { | ||
| 235 | - exception.printStackTrace(); | ||
| 236 | - } | ||
| 237 | - } | ||
| 238 | - return null; | ||
| 239 | - } | ||
| 240 | - | ||
| 241 | - | ||
| 242 | - /** | ||
| 243 | - * 发送 POST 请求(HTTP),不带输入数据 | ||
| 244 | - * | ||
| 245 | - * @param apiUrl | ||
| 246 | - * @return | ||
| 247 | - */ | ||
| 248 | - public static String doPost(String apiUrl) { | ||
| 249 | - return doPost(apiUrl, "", new HashMap<>()); | ||
| 250 | - } | ||
| 251 | - | ||
| 252 | - /** | ||
| 253 | - * 发送 POST 请求,K-V形式 | ||
| 254 | - * | ||
| 255 | - * @param apiUrl API接口URL | ||
| 256 | - * @return | ||
| 257 | - */ | ||
| 258 | - public static String doPost(String apiUrl, String paramStr, Map<String, String> headers) { | ||
| 259 | - CloseableHttpClient httpClient = null; | ||
| 260 | - if (apiUrl.startsWith("https")) { | ||
| 261 | - httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()) | ||
| 262 | - .setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build(); | ||
| 263 | - } else { | ||
| 264 | - httpClient = HttpClients.createDefault(); | ||
| 265 | - } | ||
| 266 | - String httpStr = null; | ||
| 267 | - HttpPost httpPost = new HttpPost(apiUrl); | ||
| 268 | - CloseableHttpResponse response = null; | ||
| 269 | - | ||
| 270 | - try { | ||
| 271 | - httpPost.setConfig(requestConfig); | ||
| 272 | - if(CollectionUtils.isNotEmpty(headers.entrySet())){ | ||
| 273 | - for (Map.Entry<String, String> header : headers.entrySet()) { | ||
| 274 | - httpPost.addHeader(header.getKey(), header.getValue()); | ||
| 275 | - } | ||
| 276 | - } | ||
| 277 | - httpPost.setEntity(new StringEntity(paramStr)); | ||
| 278 | - response = httpClient.execute(httpPost); | ||
| 279 | - HttpEntity entity = response.getEntity(); | ||
| 280 | - httpStr = EntityUtils.toString(entity, "UTF-8"); | ||
| 281 | - } catch (IOException e) { | ||
| 282 | - log.error("Http调用异常:{}",e.getMessage()); | ||
| 283 | - e.printStackTrace(); | ||
| 284 | - } finally { | ||
| 285 | - try { | ||
| 286 | - if (httpClient != null) { | ||
| 287 | - httpClient.close(); | ||
| 288 | - } | ||
| 289 | - if (response != null) { | ||
| 290 | - EntityUtils.consume(response.getEntity()); | ||
| 291 | - } | ||
| 292 | - } catch (IOException exception) { | ||
| 293 | - exception.printStackTrace(); | ||
| 294 | - } | ||
| 295 | - } | ||
| 296 | - return httpStr; | ||
| 297 | - } | ||
| 298 | - | ||
| 299 | - public static HttpResponse doPostAndReturnResponse(String apiUrl, Map<String, String> params, Map<String, String> headers) { | ||
| 300 | - CloseableHttpClient httpClient = null; | ||
| 301 | - if (apiUrl.startsWith("https")) { | ||
| 302 | - httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()) | ||
| 303 | - .setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build(); | ||
| 304 | - } else { | ||
| 305 | - httpClient = HttpClients.createDefault(); | ||
| 306 | - } | ||
| 307 | - HttpPost httpPost = new HttpPost(apiUrl); | ||
| 308 | - CloseableHttpResponse response = null; | ||
| 309 | - | ||
| 310 | - try { | ||
| 311 | - httpPost.setConfig(requestConfig); | ||
| 312 | - for (Map.Entry<String, String> header : headers.entrySet()) { | ||
| 313 | - httpPost.addHeader(header.getKey(), header.getValue()); | ||
| 314 | - } | ||
| 315 | - //组织请求参数 | ||
| 316 | - List<NameValuePair> paramList = new ArrayList<NameValuePair>(); | ||
| 317 | - if (params != null && params.size() > 0) { | ||
| 318 | - Set<String> keySet = params.keySet(); | ||
| 319 | - for (String key : keySet) { | ||
| 320 | - paramList.add(new BasicNameValuePair(key, params.get(key))); | ||
| 321 | - } | ||
| 322 | - } | ||
| 323 | - httpPost.setEntity(new UrlEncodedFormEntity(paramList, "UTF-8")); | ||
| 324 | - response = httpClient.execute(httpPost); | ||
| 325 | - return response; | ||
| 326 | - } catch (IOException e) { | ||
| 327 | - log.error("发送Http Post异常:{}", e); | ||
| 328 | - } finally { | ||
| 329 | - try { | ||
| 330 | - if (httpClient != null) { | ||
| 331 | - // httpClient.close(); | ||
| 332 | - } | ||
| 333 | - if (response != null) { | ||
| 334 | - EntityUtils.consume(response.getEntity()); | ||
| 335 | - } | ||
| 336 | - } catch (IOException exception) { | ||
| 337 | - exception.printStackTrace(); | ||
| 338 | - } | ||
| 339 | - } | ||
| 340 | - return null; | ||
| 341 | - } | ||
| 342 | - | ||
| 343 | - /** | ||
| 344 | - * 发送 POST 请求,JSON形式,接收端需要支持json形式,否则取不到数据 | ||
| 345 | - * | ||
| 346 | - * @param apiUrl | ||
| 347 | - * @param json json对象 | ||
| 348 | - * @return | ||
| 349 | - */ | ||
| 350 | - public static String doPost(String apiUrl, String json) { | ||
| 351 | - CloseableHttpClient httpClient = null; | ||
| 352 | - if (apiUrl.startsWith("https")) { | ||
| 353 | - httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build(); | ||
| 354 | - } else { | ||
| 355 | - httpClient = HttpClients.createDefault(); | ||
| 356 | - } | ||
| 357 | - String httpStr = null; | ||
| 358 | - HttpPost httpPost = new HttpPost(apiUrl); | ||
| 359 | - CloseableHttpResponse response = null; | ||
| 360 | - | ||
| 361 | - try { | ||
| 362 | - httpPost.setConfig(requestConfig); | ||
| 363 | - StringEntity stringEntity = new StringEntity(json, "UTF-8");// 解决中文乱码问题 | ||
| 364 | - stringEntity.setContentEncoding("UTF-8"); | ||
| 365 | - stringEntity.setContentType("application/json"); | ||
| 366 | - httpPost.setEntity(stringEntity); | ||
| 367 | - response = httpClient.execute(httpPost); | ||
| 368 | - HttpEntity entity = response.getEntity(); | ||
| 369 | - httpStr = EntityUtils.toString(entity, "UTF-8"); | ||
| 370 | - } catch (IOException e) { | ||
| 371 | - log.error("发送Http Post异常:{}}", e); | ||
| 372 | - } finally { | ||
| 373 | - try { | ||
| 374 | - if (response != null) { | ||
| 375 | - EntityUtils.consume(response.getEntity()); | ||
| 376 | - } | ||
| 377 | - } catch (IOException exception) { | ||
| 378 | - exception.printStackTrace(); | ||
| 379 | - } | ||
| 380 | - } | ||
| 381 | - return httpStr; | ||
| 382 | - } | ||
| 383 | - | ||
| 384 | - /** | ||
| 385 | - * 创建SSL安全连接 | ||
| 386 | - * | ||
| 387 | - * @return | ||
| 388 | - */ | ||
| 389 | - private static SSLConnectionSocketFactory createSSLConnSocketFactory() { | ||
| 390 | - SSLConnectionSocketFactory sslsf = null; | ||
| 391 | - try { | ||
| 392 | - SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { | ||
| 393 | - | ||
| 394 | - public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { | ||
| 395 | - return true; | ||
| 396 | - } | ||
| 397 | - }).build(); | ||
| 398 | - sslsf = new SSLConnectionSocketFactory(sslContext, new HostnameVerifier() { | ||
| 399 | - | ||
| 400 | - @Override | ||
| 401 | - public boolean verify(String arg0, SSLSession arg1) { | ||
| 402 | - return true; | ||
| 403 | - } | ||
| 404 | - }); | ||
| 405 | - } catch (GeneralSecurityException e) { | ||
| 406 | - e.printStackTrace(); | ||
| 407 | - } | ||
| 408 | - return sslsf; | ||
| 409 | - } | ||
| 410 | - | ||
| 411 | - | ||
| 412 | -} |