Explorar o código

一体化云端:
修改给北京中央研究院上传sftp,sftp目录为空的时候,创建目录失败问题

zhangchenglong hai 1 ano
pai
achega
3cdecca23d

+ 53 - 14
in-cloud/src/main/java/com/jiayue/insu/incloud/utils/SFTPUtil.java

@@ -168,16 +168,29 @@ public class SFTPUtil {
      * @throws SftpException
      * @throws Exception
      */
-    public void upload(String directory, String sftpFileName, InputStream input) throws SftpException {
+    public void upload(String directory, String sftpFileName, InputStream input) {
         try {
+            // 切换路径
             sftp.cd(directory);
         } catch (SftpException e) {
-            log.warn("directory is not exist");
-            sftp.mkdir(directory);
-            sftp.cd(directory);
+            log.warn(directory + ":directory is not exist");
+            try {
+                // 循环创建路径
+                this.createParentDirectories(sftp, directory);
+                // 切换路径
+                sftp.cd(directory);
+            } catch (SftpException e1) {
+                log.warn("sftp文件路径异常" + e1.toString());
+                return;
+            }
+        }
+        try {
+            sftp.put(input, sftpFileName);
+            log.info("file:{} is upload successful", sftpFileName);
+        } catch (SftpException e) {
+            log.warn("文件传输异常" + e.toString());
+            return;
         }
-        sftp.put(input, sftpFileName);
-        log.info("file:{} is upload successful", sftpFileName);
     }
 
     /**
@@ -289,15 +302,14 @@ public class SFTPUtil {
 //        SFTPUtil sftp = new SFTPUtil("jyzx2023", "hH6*7E$h", "114.115.165.178", 22);
 //        sftp.login();
 //        File file = new File("..\\sftp测试.txt");
-//        System.out.println(System.getProperty("user.dir"));
+//        log.info(System.getProperty("user.dir"));
 //        InputStream is = new FileInputStream(file);
 //        // 多级目录创建并上传
-//        sftp.upload("/subdata/20230620/real", "sftp测试.txt", is);
+//        sftp.upload("/subdata/20230712/real", "sftp测试.txt", is);
 //        sftp.logout();
 //    }
 
     /**
-     *
      * @param userName
      * @param password
      * @param host
@@ -306,14 +318,41 @@ public class SFTPUtil {
      * @throws SftpException
      * @throws FileNotFoundException
      */
-    public void uploadByFileAndPath(String userName ,String password ,String host ,int port ,File file,String sftpFilePath, String fileName) throws SftpException, FileNotFoundException {
+    public void uploadByFileAndPath(String userName, String password, String host, int port, File file, String sftpFilePath, String fileName) {
         SFTPUtil sftp = new SFTPUtil(userName, password, host, port);
         sftp.login();
-        System.out.println(System.getProperty("user.dir"));
-        InputStream is = new FileInputStream(file);
-        // 多级目录创建并上传
-        sftp.upload(sftpFilePath, fileName, is);
+        log.info(System.getProperty("user.dir"));
+        try {
+            InputStream is = new FileInputStream(file);
+            // 多级目录创建并上传
+            sftp.upload(sftpFilePath, fileName, is);
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        }
         sftp.logout();
     }
 
+    /**
+     * 循环创建路径
+     *
+     * @param sftpChannel
+     * @param directory
+     */
+    private static void createParentDirectories(ChannelSftp sftpChannel, String directory) {
+        String[] directories = directory.split("/");
+        String parentDirectory = "";
+
+        for (String dir : directories) {
+            if (!dir.isEmpty()) {
+                parentDirectory += "/" + dir;
+                try {
+                    sftpChannel.mkdir(parentDirectory);
+                } catch (Exception e) {
+                    // 目录已存在,忽略异常
+                }
+            }
+        }
+        log.info("创建路径:" + parentDirectory);
+    }
+
 }