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