标签:byte java contex ack post frame mapping param pack
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()));
}
} 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);
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;
private int port;
private String username;
private String password;
private String baseDir;
private int bufferSize = 1024 * 1024 * 4;
}
@Data
class 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;
}
}
标签:byte java contex ack post frame mapping param pack
原文地址:https://www.cnblogs.com/JaxYoun/p/13204753.html