فهرست منبع

提供 x11 local cleint

zhzhenqin 4 سال پیش
والد
کامیت
c6dae97d0a

+ 9 - 8
java-sftp-ftp/FtpClientHandler.java

@@ -1,4 +1,4 @@
-package ftp;
+package com.primeton.damp.fileclient;
 
 import org.apache.commons.net.ftp.FTPClient;
 import org.apache.commons.net.ftp.FTPFile;
@@ -21,7 +21,7 @@ import java.util.stream.Collectors;
 /**
  * Created by hadoop on 2018/8/9.
  */
-public class FtpClientHandler implements P11ClientHandler {
+public class FtpClientHandler implements X11ClientHandler {
 
     protected static Logger logger = LoggerFactory.getLogger("FtpClientHandler");
 
@@ -166,8 +166,8 @@ public class FtpClientHandler implements P11ClientHandler {
     @Override
     public boolean exists(String path) {
         try {
-            FTPFile mdtmFile = ftpClient.mlistFile(path);
-            return mdtmFile != null;
+            final FTPFile ftpFile = ftpClient.mdtmFile(path);
+            return ftpFile != null && ftpFile.isValid();
         } catch (IOException e) {
         	logger.error(e.getMessage());
         }
@@ -187,7 +187,7 @@ public class FtpClientHandler implements P11ClientHandler {
         if(existsDir(parentPath.toString())){
             return mkdir(path1.toString()); // 创建当前文件夹
         } else {
-            if(mkdirs(parentPath.toString())) { // 创建父级目录
+            if("/".equals(parentPath.toString()) || mkdirs(parentPath.toString())) { // 创建父级目录
                 return mkdir(path1.toString()); // 创建当前文件夹
             }
         }
@@ -237,13 +237,13 @@ public class FtpClientHandler implements P11ClientHandler {
     //test
     public static void main(String[] args) throws IOException {
         Properties map = new Properties();
-        String host = "localhost";
+        String host = "192.168.30.143";
         int port = 2121;
         String username = "admin";
-        String password = "admin";
+        String password = "primeton000000";
         FtpClientHandler handler = new FtpClientHandler(host, port, username, password, map);
 //        handler.changeWorkingDirectory("/sms");
-        List<Path> result = handler.getChildren("/ta-lib");
+        List<Path> result = handler.getChildren("/DAMP");
         for (Path ftpFile : result) {
             System.out.println(ftpFile.toString());
         }
@@ -255,6 +255,7 @@ public class FtpClientHandler implements P11ClientHandler {
         out.close();
         */
 
+        System.out.println(handler.exists("/"));
         System.out.println(handler.exists("/hello.txt"));
         System.out.println(handler.exists("/example_data_1225"));
         System.out.println(handler.exists("/world/example_data_1225"));

+ 137 - 0
java-sftp-ftp/LocalFsClientHandler.java

@@ -0,0 +1,137 @@
+package com.primeton.damp.fileclient;
+
+import org.apache.commons.compress.utils.IOUtils;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+/**
+ *
+ * 本地文件系统 模拟 X11
+ *
+ * <pre>
+ *
+ * Created by zhaopx.
+ * User: zhaopx
+ * Date: 2021/3/31
+ * Time: 09:38
+ *
+ * </pre>
+ *
+ * @author zhaopx
+ */
+public class LocalFsClientHandler implements X11ClientHandler {
+
+
+    /**
+     * 执行路径的基础路径
+     */
+    private final String basePath;
+
+
+    /**
+     * 本地文件系统
+     */
+    private final static FileSystem fs = FileSystems.getDefault();
+
+
+    public LocalFsClientHandler(String basePath) {
+        this.basePath = basePath;
+    }
+
+    @Override
+    public void reconnect(Properties params) throws IOException {
+
+    }
+
+    @Override
+    public OutputStream writeFile(String file, boolean overwrite) throws IOException {
+        final Path fsPath = fs.getPath(basePath, file);
+        return new FileOutputStream(fsPath.toFile(), !overwrite);
+    }
+
+    @Override
+    public boolean mkdir(String path) {
+        final Path fsPath = fs.getPath(basePath, path);
+        try {
+            Files.createDirectory(fsPath);
+            return existsDir(path);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public boolean mkdirs(String path) {
+        final Path fsPath = fs.getPath(basePath, path);
+        return fsPath.toFile().mkdirs();
+    }
+
+    @Override
+    public List<Path> getChildren(String ftpPath) {
+        final Path fsPath = fs.getPath(basePath, ftpPath);
+        if(Files.exists(fsPath) && Files.isDirectory(fsPath)) {
+            try {
+                return Files.list(fsPath).collect(Collectors.toList());
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
+        return Collections.emptyList();
+    }
+
+    @Override
+    public boolean exists(String path) {
+        final Path fsPath = fs.getPath(basePath, path);
+        return Files.exists(fsPath);
+    }
+
+    @Override
+    public boolean existsDir(String path) {
+        final Path fsPath = fs.getPath(basePath, path);
+        return Files.exists(fsPath) && Files.isDirectory(fsPath);
+    }
+
+    @Override
+    public boolean existsFile(String path) {
+        final Path fsPath = fs.getPath(basePath, path);
+        return Files.exists(fsPath) && Files.isRegularFile(fsPath);
+    }
+
+    @Override
+    public void close() throws IOException {
+
+    }
+
+
+    //test
+    public static void main(String[] args) throws IOException {
+        String basePath = "/Volumes/Media/Primeton/CODE/damp/damp-server/manual";
+        LocalFsClientHandler handler = new LocalFsClientHandler(basePath);
+        List<Path> result = handler.getChildren("/");
+        for (Path ftpFile : result) {
+            System.out.println(ftpFile.toString());
+        }
+
+        System.out.println(handler.exists("/"));
+        System.out.println(handler.exists("/hello.txt"));
+        System.out.println(handler.exists("/example_data_1225"));
+        System.out.println(handler.exists("/world/example_data_1225"));
+
+        if(!handler.existsDir("/world/example_data_1225")) {
+            System.out.println(handler.mkdirs("/world/example_data_1225"));
+        }
+        System.out.println(handler.exists("/world/example_data_1225"));
+
+    }
+}

+ 2 - 2
java-sftp-ftp/SftpClientHandler.java

@@ -1,4 +1,4 @@
-package ftp;
+package com.primeton.damp.fileclient;
 
 import com.google.common.collect.Lists;
 import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystemProvider;
@@ -17,7 +17,7 @@ import static java.nio.file.StandardOpenOption.CREATE;
 /**
  * Created by hadoop on 2018/8/9.
  */
-public class SftpClientHandler implements P11ClientHandler {
+public class SftpClientHandler implements X11ClientHandler {
 
     protected static Logger logger = LoggerFactory.getLogger("SftpClientHandler");
 

+ 3 - 3
java-sftp-ftp/P11ClientHandler.java → java-sftp-ftp/X11ClientHandler.java

@@ -1,4 +1,4 @@
-package ftp;
+package com.primeton.damp.fileclient;
 
 import java.io.Closeable;
 import java.io.IOException;
@@ -10,12 +10,12 @@ import java.util.Properties;
 /**
  *
  *
- * 太保家园,P11 连接。 可选的子实现:FTP, SFTP 协议。使用完成请关闭 close
+ * X11 连接。 可选的子实现:FTP, SFTP 协议。使用完成请关闭 close
  *
  *
  * Created by 59850 on 2019/3/28.
  */
-public interface P11ClientHandler extends Closeable {
+public interface X11ClientHandler extends Closeable {
 
 
     /**

+ 5 - 7
java-sftp-ftp/package-info.java

@@ -1,18 +1,16 @@
 /**
  *
- * P11
+ * P11. 文件共享
  *
  * <pre>
  *
- * Created by zhenqin.
- * User: zhenqin
+ * Created by zhaopx.
+ * User: zhaopx
  * Date: 2019/3/15
  * Time: 18:17
- * Vendor: yiidata.com
- * To change this template use File | Settings | File Templates.
  *
  * </pre>
  *
- * @author zhenqin
+ * @author zhaopx
  */
-package ftp;
+package com.primeton.damp.fileclient;