LocalFsClientHandler.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.primeton.damp.fileclient;
  2. import org.apache.commons.io.FileUtils;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.nio.file.FileSystem;
  10. import java.nio.file.FileSystems;
  11. import java.nio.file.Files;
  12. import java.nio.file.Path;
  13. import java.util.Collections;
  14. import java.util.List;
  15. import java.util.Properties;
  16. import java.util.stream.Collectors;
  17. /**
  18. *
  19. * 本地文件系统 模拟 X11
  20. *
  21. * <pre>
  22. *
  23. * Created by zhaopx.
  24. * User: zhaopx
  25. * Date: 2021/3/31
  26. * Time: 09:38
  27. *
  28. * </pre>
  29. *
  30. * @author zhaopx
  31. */
  32. public class LocalFsClientHandler implements X11ClientHandler {
  33. protected static Logger logger = LoggerFactory.getLogger("LocalFsClientHandler");
  34. /**
  35. * 执行路径的基础路径
  36. */
  37. private final String basePath;
  38. /**
  39. * 本地文件系统
  40. */
  41. private final static FileSystem fs = FileSystems.getDefault();
  42. public LocalFsClientHandler(String basePath) {
  43. this.basePath = basePath;
  44. }
  45. @Override
  46. public void reconnect(Properties params) throws IOException {
  47. }
  48. @Override
  49. public OutputStream writeFile(String file, boolean overwrite) throws IOException {
  50. final Path fsPath = fs.getPath(basePath, file);
  51. return new FileOutputStream(fsPath.toFile(), !overwrite);
  52. }
  53. @Override
  54. public InputStream readFile(String file) throws IOException {
  55. return Files.newInputStream(fs.getPath(basePath, file));
  56. }
  57. @Override
  58. public boolean rename(String file, String newFileName) {
  59. try {
  60. Path path1 = fs.getPath(file);
  61. Path path = fs.getPath(path1.getParent().toString(), newFileName);
  62. Files.move(path1, path);
  63. logger.info("rename {} to {}", path1.toString(), path.toString());
  64. return true;
  65. } catch (Exception e) {
  66. logger.error("Error in rename ftp file.", e);
  67. return false;
  68. }
  69. }
  70. @Override
  71. public boolean deleteFile(String path) {
  72. final Path fsPath = fs.getPath(basePath, path);
  73. try {
  74. if (Files.exists(fsPath) && Files.isDirectory(fsPath)) {
  75. // 删除文件夹,连带文件夹一起删除,包括其下的子文件
  76. FileUtils.deleteDirectory(fsPath.toFile());
  77. } else {
  78. // 删除文件
  79. Files.delete(fsPath);
  80. }
  81. } catch (Exception e) {
  82. throw new RuntimeException(e);
  83. }
  84. return true;
  85. }
  86. @Override
  87. public boolean mkdir(String path) {
  88. final Path fsPath = fs.getPath(basePath, path);
  89. try {
  90. Files.createDirectory(fsPath);
  91. return existsDir(path);
  92. } catch (IOException e) {
  93. throw new RuntimeException(e);
  94. }
  95. }
  96. @Override
  97. public boolean mkdirs(String path) {
  98. final Path fsPath = fs.getPath(basePath, path);
  99. return fsPath.toFile().mkdirs();
  100. }
  101. @Override
  102. public List<Path> getChildren(String ftpPath) {
  103. final Path fsPath = fs.getPath(basePath, ftpPath);
  104. if(Files.exists(fsPath) && Files.isDirectory(fsPath)) {
  105. try {
  106. return Files.list(fsPath).collect(Collectors.toList());
  107. } catch (Exception e) {
  108. throw new RuntimeException(e);
  109. }
  110. }
  111. return Collections.emptyList();
  112. }
  113. @Override
  114. public boolean exists(String path) {
  115. final Path fsPath = fs.getPath(basePath, path);
  116. return Files.exists(fsPath);
  117. }
  118. @Override
  119. public boolean existsDir(String path) {
  120. final Path fsPath = fs.getPath(basePath, path);
  121. return Files.exists(fsPath) && Files.isDirectory(fsPath);
  122. }
  123. @Override
  124. public boolean existsFile(String path) {
  125. final Path fsPath = fs.getPath(basePath, path);
  126. return Files.exists(fsPath) && Files.isRegularFile(fsPath);
  127. }
  128. @Override
  129. public void close() throws IOException {
  130. }
  131. }