|
@@ -0,0 +1,183 @@
|
|
|
|
+package com.sooka.sponest.data.utils;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.ruoyi.common.core.utils.DateUtils;
|
|
|
|
+import com.ruoyi.common.core.utils.SpringUtils;
|
|
|
|
+import com.ruoyi.common.core.utils.file.FilePrefixUtils;
|
|
|
|
+import com.ruoyi.common.redis.service.RedisService;
|
|
|
|
+import com.ruoyi.system.api.RemoteConfigService;
|
|
|
|
+import com.ruoyi.system.api.RemoteFileService;
|
|
|
|
+import com.ruoyi.system.api.domain.SysFile;
|
|
|
|
+import com.sooka.sponest.data.system.attach.domain.CenterdataTAttach;
|
|
|
|
+import com.sooka.sponest.data.system.attach.mapper.CenterdataTAttachMapper;
|
|
|
|
+import com.sooka.sponest.data.system.attach.service.ICenterdataTAttachService;
|
|
|
|
+import org.apache.commons.collections4.MapUtils;
|
|
|
|
+import org.apache.commons.fileupload.FileItem;
|
|
|
|
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.io.*;
|
|
|
|
+import java.net.HttpURLConnection;
|
|
|
|
+import java.net.URL;
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+@Configuration
|
|
|
|
+@EnableScheduling
|
|
|
|
+//@RestController
|
|
|
|
+//@RequestMapping("/testattach")
|
|
|
|
+public class ScheduleAtachUtil {
|
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ICenterdataTAttachService centerdataTAttachService;
|
|
|
|
+ @Resource
|
|
|
|
+ private RedisService redisService;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @GetMapping("/attach")
|
|
|
|
+ public void runAtach() throws Exception {
|
|
|
|
+ logger.info("执行定时任务大华图片本地上传开始=>{}", DateUtils.dateTimeNow());
|
|
|
|
+ Map<String, String> paramMap = SpringUtils.getBean(RemoteConfigService.class).remotegetConfigMap(Arrays.asList(EventConstants.FILE_PREFIX_LOCAL, EventConstants.FILE_PREFIX_HIK1, EventConstants.FILE_PREFIX_HIK2, EventConstants.FILE_PREFIX_DH1, EventConstants.FILE_PREFIX_DH2)).getData();
|
|
|
|
+ //paramMap.put("token", redisService.getCacheObject("DH_TOKEN"));// 大华token
|
|
|
|
+ List<Map<String, Object>> list = centerdataTAttachService.selectFailUpattah();
|
|
|
|
+ int i = 0;
|
|
|
|
+ for (Map map : list) {
|
|
|
|
+ paramMap.put("czlx", "AI");
|
|
|
|
+ paramMap.put("busId", MapUtils.getString(map, "busId"));
|
|
|
|
+ logger.info("event_name-------------->"+MapUtils.getString(map, "event_name"));
|
|
|
|
+ logger.info("create_time-------------->"+MapUtils.getString(map, "create_time"));
|
|
|
|
+ logger.info("==================>大华图片远程接口之前" + MapUtils.getString(map, "busId") + "当前序号" + i);
|
|
|
|
+ centerdataTAttachService.updateEventAttach(paramMap);
|
|
|
|
+ logger.info("延时1分钟");
|
|
|
|
+ i++;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ logger.info("执行定时任务大华图片本地上传结束=====>{}", DateUtils.dateTimeNow());
|
|
|
|
+ }
|
|
|
|
+ //每周的周日晚上8点开始运行
|
|
|
|
+ @Scheduled(cron = "* * 20 ? * 1")
|
|
|
|
+ @GetMapping("/urlToMultipartFile")
|
|
|
|
+ public void urlToMultipartFile() throws Exception{
|
|
|
|
+ logger.info("执行定时任务大华图片本地上传开始=>{}", DateUtils.dateTimeNow());
|
|
|
|
+ Map<String, String> paramMap = SpringUtils.getBean(RemoteConfigService.class).remotegetConfigMap(Arrays.asList(EventConstants.FILE_PREFIX_LOCAL, EventConstants.FILE_PREFIX_HIK1, EventConstants.FILE_PREFIX_HIK2, EventConstants.FILE_PREFIX_DH1, EventConstants.FILE_PREFIX_DH2)).getData();
|
|
|
|
+ List<Map<String, Object>> attaches = centerdataTAttachService.selectFailUpattah();
|
|
|
|
+ logger.info("此次需要更新的图片的总数==================>" + attaches.size());
|
|
|
|
+ int i = 0;
|
|
|
|
+ for (Map<String, Object> item : attaches) {
|
|
|
|
+ logger.info("event_name-------------->"+MapUtils.getString(item, "event_name"));
|
|
|
|
+ logger.info("create_time-------------->"+MapUtils.getString(item, "create_time"));
|
|
|
|
+ logger.info("busId==================>" + MapUtils.getString(item, "busId") + "当前序号" + i);
|
|
|
|
+ paramMap.put("token", redisService.getCacheObject("DH_TOKEN"));// 大华token
|
|
|
|
+ File file = null;
|
|
|
|
+ MultipartFile multipartFile = null;
|
|
|
|
+ String url = "";
|
|
|
|
+ if ("DH_1".equals(item.get("BusSource"))) {
|
|
|
|
+ url = FilePrefixUtils.getUrlPrefix(item.get("BusSource").toString(), paramMap) + item.get("url") + "?token=" + MapUtils.getString(paramMap, "token");
|
|
|
|
+ } else {
|
|
|
|
+ url = FilePrefixUtils.getUrlPrefix(item.get("BusSource").toString(), paramMap) + item.get("url");
|
|
|
|
+ }
|
|
|
|
+ HttpURLConnection httpUrl = null;
|
|
|
|
+ try {
|
|
|
|
+ httpUrl = (HttpURLConnection) new URL(url).openConnection();
|
|
|
|
+ // 设置连接超时时间为5秒
|
|
|
|
+ httpUrl.setConnectTimeout(5000);
|
|
|
|
+ // 设置读取超时时间为10秒
|
|
|
|
+ httpUrl.setReadTimeout(10000);
|
|
|
|
+ httpUrl.connect();
|
|
|
|
+ logger.info("url===>" + url);
|
|
|
|
+ logger.info("Code===>" + httpUrl.getResponseCode());
|
|
|
|
+ logger.info("meaasge===>" + httpUrl.getResponseMessage());
|
|
|
|
+ httpUrl.connect();
|
|
|
|
+ file = inputStreamToFile(httpUrl.getInputStream(), System.currentTimeMillis() + ".jpg");
|
|
|
|
+ multipartFile = fileToMultipartFile(file);
|
|
|
|
+ httpUrl.disconnect();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.info(e.getMessage());
|
|
|
|
+ } finally {
|
|
|
|
+ httpUrl.disconnect();
|
|
|
|
+ }
|
|
|
|
+ long l = System.currentTimeMillis();
|
|
|
|
+ logger.info("-------------<>" + l);
|
|
|
|
+ logger.info("multipartFile-------------" + multipartFile);
|
|
|
|
+ logger.info("getOriginalFilename---------------------------->" + multipartFile.getOriginalFilename());
|
|
|
|
+ if (null != multipartFile) {
|
|
|
|
+ SysFile sysFile = SpringUtils.getBean(RemoteFileService.class).upload(multipartFile).getData();
|
|
|
|
+ try {
|
|
|
|
+ Thread.sleep(30000);
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ logger.info("sysFile===========>" + sysFile);
|
|
|
|
+ logger.info("getWebUrl===========>" + sysFile.getWebUrl());
|
|
|
|
+ logger.info("====>" + (System.currentTimeMillis() - l));
|
|
|
|
+ centerdataTAttachService.updateEventAttachSchedule(item, sysFile);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ public static File inputStreamToFile(InputStream ins, String name) {
|
|
|
|
+ File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
|
|
|
|
+ int len = 8192;
|
|
|
|
+ byte[] buffer = new byte[len];
|
|
|
|
+ OutputStream os = null;
|
|
|
|
+ int bytesRead;
|
|
|
|
+ try {
|
|
|
|
+ os = new FileOutputStream(file);
|
|
|
|
+ while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
|
|
|
|
+ os.write(buffer, 0, bytesRead);
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ try {
|
|
|
|
+ os.close();
|
|
|
|
+ ins.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return file;
|
|
|
|
+ }
|
|
|
|
+ public static MultipartFile fileToMultipartFile(File file) {
|
|
|
|
+ DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(16, null);
|
|
|
|
+ FileItem item = diskFileItemFactory.createItem(file.getName(), "text/plain", true, file.getName());
|
|
|
|
+ int len = 8192;
|
|
|
|
+ byte[] buffer = new byte[len];
|
|
|
|
+ FileInputStream fis = null;
|
|
|
|
+ OutputStream os = null;
|
|
|
|
+ int bytesRead;
|
|
|
|
+ try {
|
|
|
|
+ fis = new FileInputStream(file);
|
|
|
|
+ os = item.getOutputStream();
|
|
|
|
+ while ((bytesRead = fis.read(buffer, 0, len)) != -1) {
|
|
|
|
+ os.write(buffer, 0, bytesRead);
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ try {
|
|
|
|
+ os.close();
|
|
|
|
+ fis.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return new CommonsMultipartFile(item);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|