打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
JavaFTP递归查询指定目录下的所有目录和文件
需要在前端展示ftp服务器中的目录结构,实现用户对ftp目录实时查询和其他文件操作。
采用递归方式,用户传入目录,递归查询,按层级结构封装,返回给前端,前端按层级展示给用户。
依赖apache的commons-net3.6提供的ftp功能。
下列代码可以直接当作一个spring-boot controller使用。
package com.projects.se1080.qrcode_auth.controller;import io.swagger.annotations.ApiOperation;import lombok.Data;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;import java.io.IOException;import java.util.ArrayList;import java.util.List;/** * @author:Yjx * @description: * @date:2019/6/28 19:46 */@RestController@RequestMapping("/ftp")public class RecursionFtp { @Resource private FtpConfig ftpConfig; @ApiOperation("递归列出FTP目录下的内容(包括文件和目录)") @PostMapping("/recursionDir") public MyTree recursionDir(@RequestBody ListDirDTO dto) throws Exception { FTPClient ftp = this.initFtpClient(); if (ftp.getReplyCode() > 0) { String directory = dto.getDirectory(); ftp.changeWorkingDirectory(directory); FTPFile[] ftpFiles = ftp.listFiles(); MyTree root = new MyTree(directory); if (ftpFiles.length > 0) { this.recursion(ftpFiles, ftp, root); } this.destroyFtpClient(ftp); return root; } else { return null; } } private MyTree recursion(FTPFile[] fileArr, FTPClient ftp, MyTree myTree) throws Exception { if (fileArr.length > 0) { for (FTPFile it : fileArr) { if (it.isDirectory()) { ftp.changeWorkingDirectory(new String(it.getName().getBytes("utf-8"), "iso-8859-1")); FTPFile[] ftpFiles = ftp.listFiles(); if (ftpFiles.length > 0) { myTree.getChildren().add(this.recursion(ftpFiles, ftp, new MyTree(it.getName()))); } else { myTree.getChildren().add(new MyTree(it.getName())); ftp.changeToParentDirectory(); // 空目录务必要返回上一级 } } else { myTree.getChildren().add(new MyTree(it.getName())); } } } ftp.changeToParentDirectory(); return myTree; } /** * 初始化ftpClient * * @return * @throws IOException */ private FTPClient initFtpClient() throws IOException { FTPClient ftp = new FTPClient(); ftp.connect(this.ftpConfig.getIp(), this.ftpConfig.getPort()); ftp.login(this.ftpConfig.getUsername(), this.ftpConfig.getPassword()); ftp.setBufferSize(this.ftpConfig.getBufferSize()); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); ftp.changeWorkingDirectory(this.ftpConfig.getBaseDir()); //对于某些ftp认证模式,需要配置用户的ftp根目录 return ftp; } /** * 销毁ftpClient * * @param ftpClient * @throws IOException */ private void destroyFtpClient(FTPClient ftpClient) throws IOException { ftpClient.logout(); ftpClient.disconnect(); }}/** * ftp服务器相关配置 */@Data@Configuration@ConfigurationProperties(prefix = "ftp")class FtpConfig { private String ip = "192.168.1.10"; private int port = 21; private String username = "myFtp"; private String password = "xxxxxx"; private String baseDir = "/"; private int bufferSize = 1024 * 1024 * 4;}@Dataclass MyTree { private String name; private List<MyTree> children; public MyTree() { } public MyTree(String name) { this.name = name; this.children = new ArrayList<>(); } public MyTree(String name, List<MyTree> children) { this.name = name; this.children = children; }}
学习使我充实,分享给我快乐!
语言方法
81386dAi9
i0ik7抖音0播放怎么回事「解决方式」分享
93922012/05/04 12:47:17
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
使用递归方法实现,向FTP服务器上传整个目录结构、从FTP服务器下载整个目录到本地的功能
FTP获取根目录下的所有子文件
spring boot整合Shiro实现单点登录
Spring boot Mybatis(讀寫分離配置)
Activemq+spring的第一个程序(入门程序--内嵌Broker--消息队列)
struts2.1.8版如何使用ajax
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服