Java实现FTP读取文件

创建FTP略,直接上代码

ftp:
  server: 038.438.383.388
  port: 21
  username: 038
  password: 
  
  #application.yml中添加以上配置

 

controller

 





@RestController
@RequestMapping("/ftpNodes")
@Api(value = "API - FtpNodeController", tags = "读取树")
public class FtpNodeController extends BaseController {

    @ApiOperation(value = "获取树")
    @PostMapping("/getFTPNodeListInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dirName", value = "目录名称", dataType = "string", paramType = "query")
            //,@ApiImplicitParam(name = "projectCode", value = "项目编码", dataType = "string", paramType = "query")
    })
    public Result getFTPNodeListInfo(String dirName){
        List<Node> nodeList = FtpUtil.getTree("test/" + dirName);
        return new Result(nodeList);
    }


}

 

工具类

 

/**
    * @描述: 生成目录树
    * @作者: fan
    * @日期: 2021/05/20 10:56
    **/
    public static List<Node> getTree(String path) {
        FTPClient ftp = localConn();
        if (ftp != null) {
            getExchageDirLength(path, ftp);
            String rootNodeName = path.substring(path.lastIndexOf("/") + 1);
            List<Node> nodeList = new ArrayList<>();
            Node rootNode = Node.getDirNode(getId(), rootNodeName, path,"-1");
            nodeList.add(rootNode);
            listTree(ftp, path, rootNode,nodeList);
            return nodeList;
        } else {
            return null;
        }
    }
	

Node.java

import java.util.List;


public class Node {
	
	//get,set略
	
    private String id;
    private String name;
    private String path;
    private Integer type;
    private Integer treeLevel;
    private String parentId;

    private List<Node> children;

    private Node(String id, String name, String path, Integer type, Integer treeLevel, String parentId) {
        this.id = id;
        this.name = name;
        this.path = path;
        this.type = type;
        this.treeLevel = treeLevel;
        this.parentId = parentId;
    }

public static Integer getTreeLevel2(String path){
        char[] chars = path.toCharArray();
        int level = 0;
        for(int i = 0; i < chars.length; i++){
            if(chars[i] == '/'){
                level++;
            }
        }
        return level;
    }

 public static Node getDirNode(String id, String name, String path, String parentId) {
        return new Node(id, name, path, 1, getTreeLevel(path),parentId);
 }
 public static Node getFileNode(String id, String name, String path, String parentId) {
        return new Node(id, name, path, 2, getTreeLevel2(path),parentId);
 }
    
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值