检测NAS是否启用的FTP连接模式
如果这里不启用会出现下面错误提示:
MalformedServerReplyException: Could not parse response code. Server Reply: SSH-2.0-OpenS
使用依赖
<dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>3.8.0</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.1</version></dependency>
单向树形对象
public class NasFileInfoModel {//上级目录private String parentPath;//当前目录private String path;//文件或文件夹名称private String name;//下级文件private List<NasFileInfoModel> childDir = new ArrayList<>();public NasFileInfoModel(String path, String name) {this.path = path;this.name = name;}public NasFileInfoModel() {}public String getParentPath() {return parentPath;}public void setParentPath(String parentPath) {this.parentPath = parentPath;}public String getPath() {return path;}public void setPath(String path) {this.path = path;}public String getName() {return name;}public void setName(String name) {this.name = name;}public List<NasFileInfoModel> getChildDir() {return childDir;}public void setChildDir(List<NasFileInfoModel> childDir) {this.childDir = childDir;}public void addChildDir(NasFileInfoModel childDir) {this.childDir.add(childDir);}@Overridepublic String toString() {return JSONObject.toJSONString(this);}
}
连接与赋值方法
@Testpublic void nasFileTest() {String path = "/图片素材";//指定地址目录,不指定则默认/目录//实例化服务访问对象try (Ftp ftp = new Ftp("192.168.110.69", 21, "子龙", "********.")) {FTPClient client = ftp.getClient();//将当前目录信息设置到对象中,作为第一个节点NasFileInfoModel nasFile = new NasFileInfoModel(path, path.replace("/", ""));//获取目录节点的下级文件信息List<NasFileInfoModel> nasFileInfoModels = childFileList(nasFile, client);nasFile.setChildDir(nasFileInfoModels);System.out.println("nasFile = " + nasFile);} catch (IOException e) {e.printStackTrace();}}/*** 获取文件目录的下级文件信息** @param parentFile 需要获取下级目录信息的,父节点目录* @param client tfp连接* @return 传入入节点文件的下级目录集合*/public List<NasFileInfoModel> childFileList(NasFileInfoModel parentFile, FTPClient client) throws IOException {String path = parentFile.getPath();//父目录的路径List<NasFileInfoModel> fileList = new ArrayList<>();FTPFile[] ftpFiles = client.listFiles(path);for (FTPFile ftpFile : ftpFiles) {//实例化文件信息对象,并且设置文件路径与文件名称NasFileInfoModel file = new NasFileInfoModel(path + "/" + ftpFile.getName(), ftpFile.getName());file.setParentPath(path);//设置父目录地址if (ftpFile.isDirectory()) {List<NasFileInfoModel> nasFileInfoModels = childFileList(file, client);file.setChildDir(nasFileInfoModels);}fileList.add(file);}return fileList;}
连接查询展示