lxw
2022-07-14 125b108d36eafd97fbd1d32569daf41819b5fd98
修改
7个文件已修改
3个文件已添加
343 ■■■■■ 已修改文件
.gitignore 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/constant/YamlProperties.java 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/dto/FileDirPath.java 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/dto/FtpHelper.java 221 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/DocUserService.java 13 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/FtpService.java 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/config/application-dev.yml 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/config/application-prod.yml 9 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/webapp/index.html 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
.gitignore
@@ -31,3 +31,5 @@
### VS Code ###
.vscode/
/src/main/webapp/
pom.xml
@@ -176,7 +176,11 @@
            <systemPath>${pom.basedir}/src/main/resources/lib/jacob.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.enterprisedt</groupId>
            <artifactId>edtFTPj</artifactId>
            <version>1.5.3</version>
        </dependency>
    </dependencies>
    <build>
src/main/java/com/whyc/constant/YamlProperties.java
@@ -43,6 +43,13 @@
    /**系统运行模式*/
    public static Integer runModel;
    /**FTP信息*/
    public static String ftpIp;
    public static Integer ftpPort;
    public static String ftpUserName;
    public static String ftpPassword;
    @Value("${spring.profiles.active}")
    public void setProfileType(String profileType) {
        YamlProperties.profileType = profileType;
@@ -122,5 +129,25 @@
    public void setRunModel(Integer runModel) {
        YamlProperties.runModel = runModel;
    }
    @Value("${ftpInforMation.ftpIp}")
    public void setFtpIp(String ftpIp){
        YamlProperties.ftpIp=ftpIp;
    }
    @Value("${ftpInforMation.ftpPort}")
    public void setFtpPort(Integer ftpPort){
        YamlProperties.ftpPort=ftpPort;
    }
    @Value("${ftpInforMation.ftpUserName}")
    public void setFtpUserName(String ftpUserName){
        YamlProperties.ftpUserName=ftpUserName;
    }
    @Value("${ftpInforMation.ftpPassword}")
    public void setFtpPassword(String ftpPassword){
        YamlProperties.ftpPassword=ftpPassword;
    }
}
src/main/java/com/whyc/dto/FileDirPath.java
New file
@@ -0,0 +1,22 @@
package com.whyc.dto;
import com.whyc.constant.YamlProperties;
import org.springframework.boot.system.ApplicationHome;
import java.io.File;
public class FileDirPath {
    //当前项目在dev和prod模式下访问路径
    public static String getFileDirName(){
        String fileDirName = "";
        ApplicationHome applicationHome = new ApplicationHome(FileDirPath.class);
        File jarFile = applicationHome.getDir();
        if( 1 == YamlProperties.runModel){
            fileDirName = jarFile.getParentFile().toString();
        }else{
            //打包版
            fileDirName = jarFile.toString();
        }
        return fileDirName;
    }
}
src/main/java/com/whyc/dto/FtpHelper.java
New file
@@ -0,0 +1,221 @@
package com.whyc.dto;
import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.whyc.constant.YamlProperties;
import org.apache.commons.lang.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class FtpHelper {
    private FTPClient ftp;
    /**
     * 初始化Ftp信息
     *
     * @param ftpServer   ftp服务器地址
     * @param ftpPort     Ftp端口号
     * @param ftpUsername ftp 用户名
     * @param ftpPassword ftp 密码
     */
    public FtpHelper(String ftpServer, int ftpPort, String ftpUsername,
                     String ftpPassword) {
        connect(ftpServer, ftpPort, ftpUsername, ftpPassword);
    }
    /**
     * 连接到ftp
     *
     * @param ftpServer   ftp服务器地址
     * @param ftpPort     Ftp端口号
     * @param ftpUsername ftp 用户名
     * @param ftpPassword ftp 密码
     */
    public void connect(String ftpServer, int ftpPort, String ftpUsername, String ftpPassword) {
        ftp = new FTPClient();
        try {
            ftp.setControlEncoding("UTF-8");
            ftp.setRemoteHost(ftpServer);
            ftp.setRemotePort(ftpPort);
            ftp.setTimeout(6000);
            ftp.setConnectMode(FTPConnectMode.ACTIVE);
            ftp.connect();
            ftp.login(ftpUsername, ftpPassword);
            ftp.setType(FTPTransferType.BINARY);
        } catch (Exception e) {
            e.printStackTrace();
            ftp = null;
        }
    }
    /**
     * 更改ftp路径
     *
     * @param ftp
     * @param dirName
     * @return
     */
    public boolean checkDirectory(FTPClient ftp, String dirName) {
        boolean flag;
        try {
            ftp.chdir(dirName);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }
    /**
     * 断开ftp链接
     */
    public void disconnect() {
        try {
            if (ftp.connected()) {
                ftp.quit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 读取ftp文件流
     *
     * @param filePath ftp文件路径
     * @return s
     * @throws Exception
     */
    public InputStream downloadFile(String filePath) throws Exception {
        InputStream inputStream = null;
        String fileName = "";
        filePath = StringUtils.removeStart(filePath, "/");
        int len = filePath.lastIndexOf("/");
        if (len == -1) {
            if (filePath.length() > 0) {
                fileName = filePath;
            } else {
                throw new Exception("没有输入文件路径");
            }
        } else {
            fileName = filePath.substring(len + 1);
            String type = filePath.substring(0, len);
            String[] typeArray = type.split("/");
            for (String s : typeArray) {
                ftp.chdir(s);
            }
        }
        byte[] data;
        try {
            data = ftp.get(fileName);
            inputStream = new ByteArrayInputStream(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return inputStream;
    }
    /**
     * 上传文件到ftp
     *
     * @param file     文件对象
     * @param filePath 上传的路径
     * @throws Exception
     */
    public void uploadFile(File file, String filePath) throws Exception {
        InputStream inStream = new FileInputStream(file);
        uploadFile(inStream, filePath);
    }
    /**
     * 上传文件到ftp
     *
     * @param inStream 上传的文件流
     * @param filePath 上传路径
     * @throws Exception
     */
    public void uploadFile(InputStream inStream, String filePath)
            throws Exception {
        if (inStream == null) {
            return;
        }
        String fileName = "";
        filePath = StringUtils.removeStart(filePath, "/");
        int len = filePath.lastIndexOf("/");
        if (len == -1) {
            if (filePath.length() > 0) {
                fileName = filePath;
            } else {
                throw new Exception("没有输入文件路径");
            }
        } else {
            fileName = filePath.substring(len + 1);
            String type = filePath.substring(0, len);
            String[] typeArray = type.split("/");
            for (String s : typeArray) {
                if (!checkDirectory(ftp, s)) {
                    ftp.mkdir(s);
                }
            }
        }
        ftp.put(inStream, fileName);
    }
    /**
     * 删除ftp文件
     *
     * @param filePath 文件路径
     * @throws Exception
     */
    public void deleteFile(String filePath) throws Exception {
        String fileName = "";
        filePath = StringUtils.removeStart(filePath, "/");
        int len = filePath.lastIndexOf("/");
        if (len == -1) {
            if (filePath.length() > 0) {
                fileName = filePath;
            } else {
                throw new Exception("没有输入文件路径");
            }
        } else {
            fileName = filePath.substring(len + 1);
            String type = filePath.substring(0, len);
            String[] typeArray = type.split("/");
            for (String s : typeArray) {
                if (checkDirectory(ftp, s)) {
                    ftp.chdir(s);
                }
            }
        }
        ftp.delete(fileName);
    }
    /*
     切换目录
    public void changeDirectory(String path) {
        if (!ValidateUtils.isEmpty(path)) {
            try {
                ftp.chdir(path);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }*/
    public static void main(String[] args) {
        /*try { // 从ftp下载文件
            FtpHelper ftp = new FtpHelper("192.168.10.80", 21, "lxw", "lxw810412026");
            File file = new File("D:\\1.doc");
            ftp.uploadFile(file, "test/1.doc");
            ftp.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }*/
    }
}
src/main/java/com/whyc/service/DocUserService.java
@@ -4,6 +4,7 @@
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.constant.YamlProperties;
import com.whyc.dto.FileDirPath;
import com.whyc.dto.Page;
import com.whyc.dto.Response;
import com.whyc.mapper.DocUserMapper;
@@ -61,16 +62,8 @@
    //检测是否存在重新上传的人脸
    public int checkFaceData(MultipartFile file,int faceId){
        String fileDirName = "";
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        File jarFile = applicationHome.getDir();
        if( 1 == YamlProperties.runModel){
            fileDirName = jarFile.getParentFile().toString();
        }else{
            //打包版
            fileDirName = jarFile.toString();
        }
        String root=fileDirName+"/face/"+File.separator;
        String fileDirName = FileDirPath.getFileDirName();
        String root=fileDirName+File.separator+"face"+File.separator;
        if(file.isEmpty()){
            return faceId;
        }else{
src/main/java/com/whyc/service/FtpService.java
New file
@@ -0,0 +1,33 @@
package com.whyc.service;
import com.whyc.constant.YamlProperties;
import com.whyc.dto.FileDirPath;
import com.whyc.dto.FtpHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.io.File;
@Service
@EnableScheduling   // 2.开启定时任务
public class FtpService {
    @Autowired(required = false)
    private FtpHelper ftpHelper;
    //定时上传指定目录下文件
    @Scheduled(cron = "0/20 41 16 * * ?")
    private void sendFtpFile(){
        FtpHelper ftp = new FtpHelper(YamlProperties.ftpIp, YamlProperties.ftpPort, YamlProperties.ftpUserName, YamlProperties.ftpPassword);
        String fileDirName = FileDirPath.getFileDirName();
        String root=fileDirName+File.separator+"face";
        File file = new File(root+File.separator+"2.doc");
        try {
            ftp.uploadFile(file, "test/2.doc");
        } catch (Exception e) {
            e.printStackTrace();
        }
        ftp.disconnect();
    }
}
src/main/resources/config/application-dev.yml
@@ -91,4 +91,12 @@
configFile:
  type: 1 #1:本地测试;2:打包jar
#FTP本分数据
ftpInforMation:
  ftpIp: 192.168.10.80
  ftpPort: 21
  ftpUserName: lxw
  ftpPassword: lxw810412026
src/main/resources/config/application-prod.yml
@@ -89,4 +89,11 @@
#Config文件读取 涉及License和FGCDFileDownload
configFile:
  type: 2 #1:本地测试;2:打包jar
  type: 2 #1:本地测试;2:打包jar
#FTP本分数据
ftpInforMation:
  ftpIp:192.168.10.80
  ftpPort:21
  ftpUserName:lxw
  ftpPassword:lxw810412026
src/main/webapp/index.html
@@ -5,6 +5,6 @@
    <title>Title</title>
</head>
<body>
123456
</body>
</html>