LocalFsClientHandler.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.primeton.damp.fileclient;
  2. import org.apache.commons.compress.utils.IOUtils;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.nio.file.FileSystem;
  8. import java.nio.file.FileSystems;
  9. import java.nio.file.Files;
  10. import java.nio.file.Path;
  11. import java.util.Collections;
  12. import java.util.List;
  13. import java.util.Properties;
  14. import java.util.stream.Collectors;
  15. /**
  16. *
  17. * 本地文件系统 模拟 X11
  18. *
  19. * <pre>
  20. *
  21. * Created by zhaopx.
  22. * User: zhaopx
  23. * Date: 2021/3/31
  24. * Time: 09:38
  25. *
  26. * </pre>
  27. *
  28. * @author zhaopx
  29. */
  30. public class LocalFsClientHandler implements X11ClientHandler {
  31. /**
  32. * 执行路径的基础路径
  33. */
  34. private final String basePath;
  35. /**
  36. * 本地文件系统
  37. */
  38. private final static FileSystem fs = FileSystems.getDefault();
  39. public LocalFsClientHandler(String basePath) {
  40. this.basePath = basePath;
  41. }
  42. @Override
  43. public void reconnect(Properties params) throws IOException {
  44. }
  45. @Override
  46. public OutputStream writeFile(String file, boolean overwrite) throws IOException {
  47. final Path fsPath = fs.getPath(basePath, file);
  48. return new FileOutputStream(fsPath.toFile(), !overwrite);
  49. }
  50. @Override
  51. public boolean mkdir(String path) {
  52. final Path fsPath = fs.getPath(basePath, path);
  53. try {
  54. Files.createDirectory(fsPath);
  55. return existsDir(path);
  56. } catch (IOException e) {
  57. throw new RuntimeException(e);
  58. }
  59. }
  60. @Override
  61. public boolean mkdirs(String path) {
  62. final Path fsPath = fs.getPath(basePath, path);
  63. return fsPath.toFile().mkdirs();
  64. }
  65. @Override
  66. public List<Path> getChildren(String ftpPath) {
  67. final Path fsPath = fs.getPath(basePath, ftpPath);
  68. if(Files.exists(fsPath) && Files.isDirectory(fsPath)) {
  69. try {
  70. return Files.list(fsPath).collect(Collectors.toList());
  71. } catch (Exception e) {
  72. throw new RuntimeException(e);
  73. }
  74. }
  75. return Collections.emptyList();
  76. }
  77. @Override
  78. public boolean exists(String path) {
  79. final Path fsPath = fs.getPath(basePath, path);
  80. return Files.exists(fsPath);
  81. }
  82. @Override
  83. public boolean existsDir(String path) {
  84. final Path fsPath = fs.getPath(basePath, path);
  85. return Files.exists(fsPath) && Files.isDirectory(fsPath);
  86. }
  87. @Override
  88. public boolean existsFile(String path) {
  89. final Path fsPath = fs.getPath(basePath, path);
  90. return Files.exists(fsPath) && Files.isRegularFile(fsPath);
  91. }
  92. @Override
  93. public void close() throws IOException {
  94. }
  95. //test
  96. public static void main(String[] args) throws IOException {
  97. String basePath = "/Volumes/Media/Primeton/CODE/damp/damp-server/manual";
  98. LocalFsClientHandler handler = new LocalFsClientHandler(basePath);
  99. List<Path> result = handler.getChildren("/");
  100. for (Path ftpFile : result) {
  101. System.out.println(ftpFile.toString());
  102. }
  103. System.out.println(handler.exists("/"));
  104. System.out.println(handler.exists("/hello.txt"));
  105. System.out.println(handler.exists("/example_data_1225"));
  106. System.out.println(handler.exists("/world/example_data_1225"));
  107. if(!handler.existsDir("/world/example_data_1225")) {
  108. System.out.println(handler.mkdirs("/world/example_data_1225"));
  109. }
  110. System.out.println(handler.exists("/world/example_data_1225"));
  111. }
  112. }