duhuizhe
2024-04-17 c389984b5d3e5523e1b22de1fc045dc415261330
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.yqzx.common.util;
 
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
import javax.imageio.ImageIO;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
 
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.ssh.JschUtil;
import cn.hutool.extra.ssh.Sftp;
 
/**
 * @author ChenLong
 * @version 1.0
 * @ClassName SFtpUtil
 * @date 2019/7/24 20:53
 * @Description TODO
 */
@Slf4j
public class SftpUtil {
 
    public static Properties getFtpProp() {
        Properties props = new Properties();
        InputStream in = null;
        try {
            ResourceLoader resourceLoader = new DefaultResourceLoader();
            in = resourceLoader.getResource("sftp.properties").getInputStream();
            props.load(in);
            return props;
        } catch (IOException e) {
            log.error(e.getMessage());
            return props;
        } finally {
            ResourceUtil.safeClose(in);
        }
    }
 
    public static Sftp getSftp() {
        Properties props = getFtpProp();
        String url = props.getProperty("ftp.url");
        String username = props.getProperty("ftp.username");
        String mixpd = props.getProperty("ftp.mixpd");
        return JschUtil.createSftp(url, 22, username, mixpd);
    }
 
    public static void upload(Sftp sftp, String sourceFilePath, String targetFilePath) {
        String targetPath = targetFilePath.substring(0, targetFilePath.lastIndexOf("/"));
        sftp.mkDirs(targetPath);
        sftp.put(sourceFilePath, targetFilePath);
        FileUtil.del(sourceFilePath);
    }
 
    public static void upload(Sftp sftp, String FileName, InputStream inputStream, String targetPath) {
        if (FileName.lastIndexOf(".") <= -1) {
            return;
        }
        String ext = FileName.substring(FileName.lastIndexOf(".") + 1);
        String sourceFilePath = System.getProperty("user.dir") + ResourceUtil.createFileName(ext);
        File tmpFile = FileUtil.touch(sourceFilePath);
        FileUtil.writeFromStream(inputStream, tmpFile.getPath());
        upload(sftp, sourceFilePath, targetPath + FileName);
    }
 
    public static void upload(Sftp sftp, String FileName, BufferedImage bufferedImage, String targetPath)
            throws IOException {
        if (FileName.lastIndexOf(".") <= -1) {
            return;
        }
        String ext = FileName.substring(FileName.lastIndexOf(".") + 1);
        String sourceFilePath = System.getProperty("user.dir") + ResourceUtil.createFileName(ext);
        File tmpFile = FileUtil.touch(sourceFilePath);
        ImageIO.write(bufferedImage, "png", tmpFile);
        upload(sftp, sourceFilePath, targetPath + FileName);
    }
 
    public static void download(Sftp sftp, String sourceFilePath, String targetFilePath) {
        sftp.download(sourceFilePath, FileUtil.touch(targetFilePath));
    }
 
    public static void close(Sftp sftp) {
        if (sftp != null) {
            sftp.close();
        }
    }
}