seyason 2 лет назад
Родитель
Сommit
746cc8378c

+ 6 - 0
fhKeeper/formulahousekeeper/management-platform/pom.xml

@@ -179,6 +179,12 @@
             <version>3.3.0</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.jcraft</groupId>
+            <artifactId>jsch</artifactId>
+            <version>0.1.55</version>
+        </dependency>
+
         <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>dysmsapi20170525</artifactId>

+ 9 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/CommonUploadController.java

@@ -1,13 +1,16 @@
 package com.management.platform.controller;
 
+import com.management.platform.task.SFTPAsyncUploader;
 import com.management.platform.util.HttpRespMsg;
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
+import javax.annotation.Resource;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.InputStream;
@@ -21,6 +24,8 @@ public class CommonUploadController {
     Logger logger = LogManager.getLogger(org.apache.logging.log4j.LogManager.ROOT_LOGGER_NAME);
     @Value(value = "${upload.path}")
     private String path;
+    @Autowired
+    public SFTPAsyncUploader sftpAsyncUploader;
 
     @RequestMapping(value="uploadFile")
     public HttpRespMsg uploadFile(MultipartFile multipartFile) {
@@ -50,6 +55,10 @@ public class CommonUploadController {
             inputStream.close();
             outputStream.close();
             msg.data = serverName;
+
+            // 上传到SFTP服务器
+            sftpAsyncUploader.uploadFileAsync(file);
+
         } catch (Exception exception) {
             exception.printStackTrace();
             logger.error(exception.getMessage());

+ 8 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/ProjectDocumentController.java

@@ -9,6 +9,7 @@ import com.management.platform.constant.Constant;
 import com.management.platform.entity.*;
 import com.management.platform.mapper.*;
 import com.management.platform.service.ProjectDocumentService;
+import com.management.platform.task.SFTPAsyncUploader;
 import com.management.platform.util.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -63,6 +64,9 @@ public class ProjectDocumentController {
     @Value("${upload.path}")
     private String uploadPath;
 
+    @Autowired
+    public SFTPAsyncUploader sftpAsyncUploader;
+
     /**
      * 上传文件
      * @param projectId 项目id
@@ -114,10 +118,14 @@ public class ProjectDocumentController {
                     try {
                         saveFile.createNewFile();
                         file.transferTo(saveFile);
+                        //异步上传到备份服务器
+                        sftpAsyncUploader.uploadFileAsync(saveFile);
+
                         //计算文件大小
                         long fileSize = saveFile.length();
                         String fileLength = FileUtil.getReadableFileSize(fileSize);
                         record.setServerName(uploadPath + fileName);
+
                         record.setSize(fileLength);
                         String pathPrefix = "/upload/";
                         record.setUrl(pathPrefix + fileName);

+ 7 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/TaskFilesController.java

@@ -5,7 +5,9 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.management.platform.entity.*;
 import com.management.platform.mapper.*;
 import com.management.platform.service.ProjectDocumentService;
+import com.management.platform.task.SFTPAsyncUploader;
 import com.management.platform.util.*;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -48,6 +50,9 @@ public class TaskFilesController {
     @Value("${upload.path}")
     private String uploadPath;
 
+    @Autowired
+    private SFTPAsyncUploader sftpAsyncUploader;
+
     /**
      * 获取该项目下的所有有效的文件列表
      * @param keyword
@@ -139,6 +144,8 @@ public class TaskFilesController {
                     try {
                         saveFile.createNewFile();
                         file.transferTo(saveFile);
+                        //异步上传到备份服务器
+                        sftpAsyncUploader.uploadFileAsync(saveFile);
                         //计算文件大小
                         long fileSize = saveFile.length();
                         String fileLength = FileUtil.getReadableFileSize(fileSize);

+ 32 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/task/SFTPAsyncUploader.java

@@ -0,0 +1,32 @@
+package com.management.platform.task;
+
+import com.management.platform.util.SFTPUploader;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Component;
+import java.io.File;
+import javax.annotation.PostConstruct;
+
+@Component
+public class SFTPAsyncUploader {
+
+    private final SFTPUploader sftpUploader;
+
+    public SFTPAsyncUploader(SFTPUploader sftpUploader) {
+        this.sftpUploader = sftpUploader;
+    }
+
+    @PostConstruct
+    public void init() {
+//        new File("uploads").mkdirs(); // 创建上传目录
+    }
+
+    @Async
+    public void uploadFileAsync(File file) {
+        try {
+            sftpUploader.uploadFile(file);
+            System.out.println("File " + file.getName() + " has been uploaded.");
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 19 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/task/TimingTask.java

@@ -30,6 +30,7 @@ import org.springframework.web.client.DefaultResponseErrorHandler;
 import org.springframework.web.client.RestTemplate;
 
 import javax.annotation.Resource;
+import java.io.File;
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.security.SecureRandom;
@@ -141,6 +142,24 @@ public class TimingTask {
         /*IntStream.rangeClosed('!', '*').forEach(VALID_TOKEN_CHARS::add);    // !-**/
     }
 
+    //每天凌晨5点10删除导出的临时文件
+    @Scheduled(cron = "0 10 05 ? * *")
+    private void deleteExportFile() {
+        if (isDev) return;
+        System.out.println("删除导出的临时文件");
+        File pathRoot = new File(path);
+        File[] files = pathRoot.listFiles();
+        //遍历files,删除名称匹配规则为以任意开头以十三位数字加.xls或.xlsx结尾的文件
+        for (File file : files) {
+            if (file.getName().matches("^[\\s\\S]*\\d{13}\\.xls[x]?$")) {
+                //删除一天前的导出文件
+                if (file.lastModified() < System.currentTimeMillis() - 24 * 60 * 60 * 1000) {
+                    file.delete();
+                }
+            }
+        }
+    }
+
     //检查项目到期,距离到期时间3天内的,每天提醒
     @Scheduled(cron = "0 0 10 ? * *")
     private void projectDeadlineAlert() {

+ 63 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/util/SFTPUploader.java

@@ -0,0 +1,63 @@
+package com.management.platform.util;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import com.jcraft.jsch.ChannelSftp;
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.Session;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+@Component
+public class SFTPUploader {
+    @Value("${sftp.isEnabled}")
+    private boolean isEnabled;
+    @Value("${sftp.server}")
+    private String server;
+
+    @Value("${sftp.port}")
+    private int port;
+    @Value("${sftp.remoteDir}")
+    private String remoteDir;
+
+    @Value("${sftp.user}")
+    private String user;
+
+    @Value("${sftp.password}")
+    private String password;
+
+    public void uploadFile(File file) throws Exception {
+        if (!isEnabled) {
+            System.out.println("sftp is not enabled, please set sftp.isEnabled=true in application.properties");
+            return;
+        }
+        JSch jsch = new JSch();
+        Session session = null;
+        ChannelSftp sftpChannel = null;
+        FileInputStream inputStream = null;
+        try {
+            session = jsch.getSession(user, server, port);
+            session.setPassword(password);
+            session.setConfig("StrictHostKeyChecking", "no");
+            session.connect();
+
+            sftpChannel = (ChannelSftp) session.openChannel("sftp");
+            sftpChannel.connect();
+
+            String remoteFilePath = (remoteDir.endsWith("/")?remoteDir: (remoteDir + "/")) + file.getName();
+            inputStream = new FileInputStream(file);
+
+            sftpChannel.put(inputStream, remoteFilePath);
+        } finally {
+            if (inputStream != null) {
+                inputStream.close();
+            }
+            if (sftpChannel != null) {
+                sftpChannel.disconnect();
+            }
+            if (session != null) {
+                session.disconnect();
+            }
+        }
+    }
+}

+ 9 - 1
fhKeeper/formulahousekeeper/management-platform/src/main/resources/application-prod.yml

@@ -102,4 +102,12 @@ management:
       enabled: false
 
 configEnv:
-  isDev: false
+  isDev: false
+# SFTP上传配置
+sftp:
+  isEnabled: true
+  server: 101.132.166.205
+  port: 22022
+  remoteDir: /bkup/timesheet
+  user: root
+  password: Huoshi@2022

+ 9 - 3
fhKeeper/formulahousekeeper/management-platform/src/main/resources/application.yml

@@ -15,7 +15,7 @@ spring:
       location: C:/upload/
   datasource:
     driver-class-name: com.mysql.cj.jdbc.Driver
-    url: jdbc:mysql://47.101.180.183:3306/man_dev?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&useSSL=false
+    url: jdbc:mysql://47.101.180.183:3306/man_hour_manager?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&useSSL=false
     username: root
     password: HuoshiDB@2022
     hikari:
@@ -150,6 +150,12 @@ privateDeployURL:
   pcUrl: http://dev.huoshishanxin.com/#/
   mobUrl: http://dev.huoshishanxin.com/#/
 
-
-
+# SFTP上传配置
+sftp:
+  isEnabled: false
+  server: 101.132.166.205
+  port: 22022
+  remoteDir: /bkup/timesheet
+  user: root
+  password: Huoshi@2022
 

+ 2 - 1
fhKeeper/formulahousekeeper/ops-platform/src/main/resources/application-dev.yml

@@ -81,7 +81,8 @@ mybatis:
 #####配置图片上传路径####
 upload:
   path: /www/staticproject/timesheet/upload/
-
+logDownLoad:
+  path: /www/webapps/worktime/wt_print.log
 ##actuator健康检查配置
 management:
   security:

+ 3 - 1
fhKeeper/formulahousekeeper/ops-platform/src/main/resources/application-prod.yml

@@ -80,8 +80,10 @@ mybatis:
   mapper-locations: mappers/*Mapper.xml
 #####配置图片上传路径####
 upload:
-  path: /www/staticproject/timesheet/upload/
+  path: /www/staticproject/octopus_vue/upload/
 
+logDownLoad:
+  path: /www/webapps/worktime/wt_print.log
 ##actuator健康检查配置
 management:
   security:

+ 25 - 0
fhKeeper/formulahousekeeper/restart.sh

@@ -0,0 +1,25 @@
+#!/bin/bash
+
+# 进程名
+process="timesheet"
+
+# 获取进程ID
+PID=$(ps -ef | grep $process | grep -v grep | awk '{print $2}')
+
+if [ -n "$PID" ]; then
+    echo "$process is exist"
+    if ps -p $PID >/dev/null; then
+        echo "$process is runnig"
+    else
+        echo " $ process is not running"
+    fi
+else
+    echo "$process is not exist"
+fi
+echo $PID;
+# 暂停1秒
+sleep 1;
+kill -9 $PID;
+echo "$process is killed"
+./startWorkTime.sh
+echo "restart timesheet success"