|
|
@@ -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"));
|
|
|
+
|
|
|
+ }
|
|
|
+}
|