xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.nuvole.util;
 
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.ssh.JschUtil;
import cn.hutool.extra.ssh.Sftp;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
/**
 * @author ChenLong
 * @version 1.0
 * @ClassName SFtpUtil
 * @date 2019/7/24 20:53
 */
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) {
            e.printStackTrace();
            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, boolean isDelSourceFile) {
        System.out.println("begin upload");
        String targetPath = targetFilePath.substring(0, targetFilePath.lastIndexOf("/"));
        System.out.println("begin mkDirs");
        sftp.mkDirs(targetPath);
        System.out.println("end mkDirs");
        sftp.put(sourceFilePath, targetFilePath);
        System.out.println("upload finish");
        if (isDelSourceFile) {
            FileUtil.del(sourceFilePath);
        }
 
    }
 
    public static void main(String[] args) {
//        String url = "116.198.40.76";
//        String username = "mysftp";
//        String mixpd = "Bjjmy_2020";
//        Sftp sftp = JschUtil.createSftp(url, 22, username, mixpd);
//        String sourceFilePath = "D:/tmp/2023/8/9/84cd0f68c1764e10a2f9307e7bd3333a/Excel测试.zip";
//        String targetFilePath = "/ecosphereShopKf/goodsUploadMsgFile/2023/8/9/84cd0f68c1764e10a2f9307e7bd3333a/Excel测试.zip";
//        upload(sftp, sourceFilePath, targetFilePath);
        String paramsStr = "材质#304不锈钢|产地#郑州";
        String[] paramsArr = paramsStr.split("\\|");
        for (String paramKV : paramsArr) {
            String[] paramKVArr = paramKV.split("#");
            if (paramKVArr.length != 2) {
                System.out.println(paramKVArr.length);
            }
            if (paramKVArr[0].length() > 20 || paramKVArr[1].length() > 300) {
                System.out.println(paramKVArr[0].length());
            }
        }
 
    }
 
    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);
        System.out.println("begin ");
        System.out.println("tmpFile.getPath(): ");
        File tmpFile = FileUtil.touch(sourceFilePath);
        FileUtil.writeFromStream(inputStream, tmpFile.getPath());
        System.out.println("tmpFile: " + tmpFile);
        upload(sftp, sourceFilePath, targetPath + FileName, true);
    }
 
    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);
        System.out.println("targetPath" + targetPath);
        upload(sftp, sourceFilePath, targetPath + FileName, true);
    }
 
    public static void download(Sftp sftp, String sourceFilePath, String targetFilePath) {
        sftp.download(sourceFilePath, FileUtil.touch(targetFilePath));
    }
 
    public static void del(Sftp sftp, String targetFilePath) {
        sftp.delFile(targetFilePath);
    }
 
    public static void close(Sftp sftp) {
        if (sftp != null) {
            sftp.close();
        }
    }
}