Procházet zdrojové kódy

公共方法增加获取镜像文件数据

xusl před 1 rokem
rodič
revize
49cb8cfb4b

+ 325 - 0
ipfcst-common/ipfcst-common-data/src/main/java/com/jiayue/ipfcst/common/data/util/CommonDataUtil.java

@@ -0,0 +1,325 @@
+package com.jiayue.ipfcst.common.data.util;
+
+import cn.hutool.core.util.CharsetUtil;
+import com.alibaba.fastjson.JSON;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.springframework.util.ResourceUtils;
+
+import java.io.*;
+import java.net.URLDecoder;
+import java.nio.charset.Charset;
+import java.nio.file.*;
+import java.nio.file.attribute.BasicFileAttributeView;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.security.MessageDigest;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 文件工具类
+ *
+ * @author xsl
+ * @version 3.0
+ */
+@Slf4j
+public class CommonDataUtil {
+  /**
+   * 读取基础镜像文件公共方法
+   * @throws Exception
+   */
+  public static Map<String,String> readBaseInfoImage() throws Exception {
+    String imageFilePath = getImageFilePath();
+    Map<String,String> map = new HashMap();
+    try (BufferedReader reader = new BufferedReader(new FileReader(imageFilePath+File.separator+"BaseInfoImage.txt"))) {
+      String line;
+      while ((line = reader.readLine()) != null) {
+        String[] spval = line.split(":::");
+        map.put(spval[0],spval[1]);
+      }
+    }
+    return map;
+  }
+
+  /**
+   * 移动本地文件
+   *
+   * @param remoteAbsoluteFile 远程文件名(包括完整路径)
+   * @param localAbsoluteFile  本地文件名(包括完整路径)
+   * @return 成功时,返回true,失败返回false
+   * @throws Exception
+   */
+  public static void move(String localAbsoluteFile, String remoteAbsoluteFile) throws Exception {
+    move(localAbsoluteFile, remoteAbsoluteFile, true);
+  }
+
+  public static void move(String localAbsoluteFile, String remoteAbsoluteFile, boolean overwrite) throws Exception {
+    File srcFile = new File(localAbsoluteFile);
+    File destDir = new File(remoteAbsoluteFile);
+    StringBuilder opts = new StringBuilder();
+    try {
+      if (!destDir.exists()) {// 如果目录不存在则创建目录
+        boolean b = destDir.mkdirs();
+        if (!b) { // 如果创建失败则抛出异常
+          throw new RuntimeException(destDir + " 目录创建失败");
+        }
+      }
+      File desFile = new File(remoteAbsoluteFile + File.separator + srcFile.getName());
+      if (overwrite && desFile.exists()) {
+        FileUtils.deleteQuietly(desFile);
+        opts.append(" >> 覆盖 >> ");
+      } else {
+        opts.append(" >> 移动 >> ");
+      }
+      FileUtils.moveFile(srcFile, desFile);
+      log.info("文件:" + srcFile.getName() + opts + destDir.getPath());
+    } catch (IOException e) {
+      throw new Exception("文件:" + srcFile.getName() + opts + destDir.getPath() + "失败。", e);
+    }
+  }
+
+  public static InputStream toInputStream(File file, Charset source, Charset target) {
+    InputStream inputStream = null;
+    try {
+      if (file.exists() && file.canRead()) {
+        String context = FileUtils.readFileToString(file, source);
+        inputStream = null == context ? null : IOUtils.toInputStream(context, target);
+      }
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+    return inputStream;
+  }
+
+  public static File convert(File file, Charset source, Charset target) {
+    if (file.exists() && file.canRead() && file.canWrite()) {
+      return CharsetUtil.convert(file, source, target);
+    }
+    return null;
+  }
+
+  //文件内容md5加密
+  public static String getMD5(byte[] fileByte) {
+    String md5 = new String();
+    try {
+      MessageDigest md = MessageDigest.getInstance("MD5");
+      md.update(fileByte);
+      byte b[] = md.digest();
+
+      int i;
+
+      StringBuffer buf = new StringBuffer("");
+      for (int offset = 0; offset < b.length; offset++) {
+        i = b[offset];
+        if (i < 0) {
+          i += 256;
+        }
+        if (i < 16) {
+          buf.append("0");
+        }
+        buf.append(Integer.toHexString(i));
+      }
+
+      md5 = buf.toString();
+
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+    return md5;
+  }
+
+  /**
+   * 获取文件创建时间
+   *
+   * @param fullFileName
+   * @return
+   */
+  public static Long getFileCreateTime(String fullFileName) {
+    Path path = Paths.get(fullFileName);
+    BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
+    BasicFileAttributes attr;
+    try {
+      attr = basicview.readAttributes();
+      Date createDate = new Date(attr.creationTime().toMillis());
+      return createDate.getTime();
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+    return System.currentTimeMillis();
+  }
+
+  /**
+   * 获取项目根路径
+   *
+   * @return
+   */
+  public static String getResourceBasePath() {
+    // 获取跟目录
+    File path = null;
+    try {
+      path = new File(ResourceUtils.getURL("classpath:").getPath());
+
+    } catch (FileNotFoundException e) {
+      // nothing to do
+    }
+    if (path == null || !path.exists()) {
+      path = new File("");
+    }
+
+    String pathStr = path.getAbsolutePath();
+    try {
+      pathStr = URLDecoder.decode(pathStr, "UTF-8");
+    } catch (UnsupportedEncodingException e) {
+      e.printStackTrace();
+    }
+    return pathStr;
+  }
+
+  /**
+   * 获取上报文件目录相对路径
+   *
+   * @return
+   */
+  public static String getFileUploadPath() {
+    return createUploadAllDir("uploadFile");
+  }
+
+  /**
+   * 获取下载文件目录相对路径
+   *
+   * @return
+   */
+  public static String getDownloadFilePath() {
+    return createUploadAllDir("downloadFile");
+  }
+
+  /**
+   * 获取下载文件目录相对路径
+   *
+   * @return
+   */
+  public static String getZxglFilePath() {
+    return createUploadAllDir("zxgl");
+  }
+
+  /**
+   * 获取日志目录相对路径
+   *
+   * @return
+   */
+  public static String getLogsPath() {
+    return createUploadAllDir("logs");
+  }
+
+  /**
+   * 获取系统脚本相对路径
+   *
+   * @return
+   */
+  public static String getBinPath() {
+    return createUploadAllDir("bin");
+  }
+
+  /**
+   * 程序备份相对路径
+   *
+   * @return
+   */
+  public static String getOriginalPath() {
+    return createUploadAllDir("original");
+  }
+
+  /**
+   * 系统资料相对路径
+   *
+   * @return
+   */
+  public static String getSystemFilePath() {
+    return createUploadAllDir("systemFile");
+  }
+
+  /**
+   * 获取被删除文件目录
+   *
+   * @return 路径
+   */
+  public static String getDeleteFilePath() {
+    return createUploadAllDir("deleteFile");
+  }
+
+  /**
+   * 程序运行相对路径
+   *
+   * @return
+   */
+  public static String getProducePath() {
+    return createUploadAllDir("produce");
+  }
+
+  /**
+   * 报表相对路径
+   *
+   * @return
+   */
+  public static String getreportFormPath() {
+    return createUploadAllDir("reportForm");
+  }
+
+  /**
+   * 镜像文件相对路径
+   *
+   * @return
+   */
+  public static String getImageFilePath() {
+    return createUploadAllDir("imageFile");
+  }
+
+  /**
+   * 获取临时文件目录相对路径
+   *
+   * @return
+   */
+  public static String getTempFilePath() {
+    return createUploadAllDir("tempFile");
+  }
+
+  public static String getE46Path() {
+    return createUploadAllDir("HN");
+  }
+
+  private static String createUploadAllDir(String dir) {
+    String path = "";
+    if (System.getProperties().getProperty("file.separator").equals("\\")) {
+      path = new File(getResourceBasePath()).getParentFile().getParentFile().getParentFile().getAbsolutePath() + File.separator + dir;
+      try {
+        path = URLDecoder.decode(path, "UTF-8");
+      } catch (UnsupportedEncodingException e) {
+        e.printStackTrace();
+      }
+      File file = new File(path);
+      if (!file.exists()) {
+        boolean b = file.mkdirs();
+        if (!b) {
+          log.error("目录创建失败" + path);
+        }
+      }
+    } else {
+      path = "/home/syjy/ipfcstV3/" + dir;
+    }
+    return path;
+  }
+
+  public static void main(String[] args) throws Exception{
+    String json="{\"area\":123,\"stationCode\":\"T00001\",\"stationStatusEnum\":\"E1\",\"altitude\":123,\"electricFieldTypeEnum\":\"E1\",\"creator\":\"admin\",\"latitude\":44.15,\"sign\":\"T00001\",\"provinceEnum\":\"E16\",\"capacity\":30,\"gridCE\":30,\"createTime\":1711343781634,\"name\":\"沈阳嘉越光伏场站\",\"netSubstationName\":\"T00001\",\"interval\":60,\"company\":\"测试\",\"location\":\"测试\",\"longitude\":118.55}";
+    String keyss = "ElectricField";
+
+    String resumeName = keyss.getClass().getCanonicalName();
+    Class newClass = Class.forName(resumeName);
+    Object obj = JSON.parseObject(json, newClass);
+
+    System.out.println(obj.toString());
+
+  }
+}

+ 1 - 1
ipfcst-console/src/main/java/com/jiayue/ipfcst/aop/BaseInfoImageAspect.java

@@ -49,7 +49,7 @@ public class BaseInfoImageAspect {
     public Object afterReturningPublish(JoinPoint joinPoint, Object methodResult) throws Throwable {
         log.info("----------------------------------进入BaseInfoImage拦截器-----------------------------------------------");
       String returnJson = JSONObject.toJSONString(methodResult);
-      Object[] args = joinPoint.getArgs();
+//      Object[] args = joinPoint.getArgs();
 
       try {
           baseInfoImageService.generateBaseInfoImage();

+ 14 - 12
ipfcst-console/src/main/java/com/jiayue/ipfcst/console/controller/ElectricFieldController.java

@@ -336,12 +336,13 @@ public class ElectricFieldController {
             log.error("系统错误:" + e.getMessage(), e);
             throw new RuntimeException(e);
         } finally {
-            if (bos != null)
-                try {
-                    bos.close();
-                } catch (IOException e) {
-                    log.error("系统错误:" + e.getMessage(), e);
-                }
+            if (bos != null) {
+              try {
+                bos.close();
+              } catch (IOException e) {
+                log.error("系统错误:" + e.getMessage(), e);
+              }
+            }
         }
     }
 
@@ -403,12 +404,13 @@ public class ElectricFieldController {
             log.error("系统错误:" + e.getMessage(), e);
             throw new RuntimeException(e);
         } finally {
-            if (bos != null)
-                try {
-                    bos.close();
-                } catch (IOException e) {
-                    log.error("系统错误:" + e.getMessage(), e);
-                }
+            if (bos != null) {
+              try {
+                bos.close();
+              } catch (IOException e) {
+                log.error("系统错误:" + e.getMessage(), e);
+              }
+            }
         }
 
     }

+ 1 - 0
ipfcst-console/src/main/java/com/jiayue/ipfcst/console/service/BaseInfoImageService.java

@@ -39,6 +39,7 @@ public class BaseInfoImageService {
       // 增加文件
       for (String str:list){
         writer.write(str);
+        writer.write(System.getProperty("line.separator"));
       }
     }
   }

+ 0 - 6
ipfcst-upload/pom.xml

@@ -33,7 +33,6 @@
         <reflections.version>0.9.10</reflections.version>
         <rxtx.version>18.12.07</rxtx.version>
         <modbus4j.version>3.0.5</modbus4j.version>
-        <commons.fileupload.version>1.3</commons.fileupload.version>
         <commons.net.version>3.3</commons.net.version>
         <velocity.version>1.7</velocity.version>
         <netty.version>4.1.22.Final</netty.version>
@@ -107,11 +106,6 @@
 <!--            <version>${commons-dbcp2.version}</version>-->
 <!--        </dependency>-->
         <dependency>
-            <groupId>commons-fileupload</groupId>
-            <artifactId>commons-fileupload</artifactId>
-            <version>${commons.fileupload.version}</version>
-        </dependency>
-        <dependency>
             <groupId>org.reflections</groupId>
             <artifactId>reflections</artifactId>
             <version>${reflections.version}</version>