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(); } } }