123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package com.primeton.damp.fileclient;
- import org.apache.commons.io.FileUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- 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 {
- protected static Logger logger = LoggerFactory.getLogger("LocalFsClientHandler");
- /**
- * 执行路径的基础路径
- */
- 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 InputStream readFile(String file) throws IOException {
- return Files.newInputStream(fs.getPath(basePath, file));
- }
- @Override
- public boolean rename(String file, String newFileName) {
- try {
- Path path1 = fs.getPath(file);
- Path path = fs.getPath(path1.getParent().toString(), newFileName);
- Files.move(path1, path);
- logger.info("rename {} to {}", path1.toString(), path.toString());
- return true;
- } catch (Exception e) {
- logger.error("Error in rename ftp file.", e);
- return false;
- }
- }
- @Override
- public boolean deleteFile(String path) {
- final Path fsPath = fs.getPath(basePath, path);
- try {
- if (Files.exists(fsPath) && Files.isDirectory(fsPath)) {
- // 删除文件夹,连带文件夹一起删除,包括其下的子文件
- FileUtils.deleteDirectory(fsPath.toFile());
- } else {
- // 删除文件
- Files.delete(fsPath);
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return true;
- }
- @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 {
- }
- }
|