This commit is contained in:
pancm 2023-06-08 11:24:25 +08:00
parent b6783b7025
commit ef5dc3deb8
3 changed files with 193 additions and 0 deletions

View File

@ -0,0 +1,96 @@
package com.pancm.ftp;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author pancm
* @Title: leakproof-server
* @Description: ftp帮助类
* @Version:1.0.0
* @Since:jdk1.8
* @date 2021/8/18
*/
public class FtpHelper {
/**
* 获取文件列表文件的属性
*
* @param ip
* @param port
* @param user
* @param pwd
* @param url
* @return
*/
public static List<Map<String, String>> getListFiles(String ip, int port, String user, String pwd, String url) throws IOException {
List<Map<String, String>> mapList = new ArrayList<>();
FTPClient ftpClient = new FTPClient();
ftpClient.connect(ip, port);
ftpClient.login(user, pwd);
FTPFile[] ftpFiles = ftpClient.listFiles(url);
if(ftpFiles!=null && ftpFiles.length>0) {
for (FTPFile ftpFile : ftpFiles) {
Map<String, String> map = new HashMap<>();
map.put("fileName",ftpFile.getName());
map.put("fileSize",getSize(ftpFile.getSize()));
map.put("fileTime",ftpFile.getTimestamp().getTime().toString());
mapList.add(map);
}
}
return mapList;
}
private static boolean testFtp(String ip, int port, String user, String pwd) throws IOException {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(ip, port);//连接ftp
ftpClient.login(user, pwd);//登陆ftp
return FTPReply.isPositiveCompletion(ftpClient.getReplyCode());
}
public static String getSize(long size) {
//获取到的size为1705230
long GB = 1024 * 1024 * 1024;//定义GB的计算常量
long MB = 1024 * 1024;//定义MB的计算常量
long KB = 1024;//定义KB的计算常量
DecimalFormat df = new DecimalFormat("0.00");//格式化小数
String resultSize = "";
if (size / GB >= 1) {
//如果当前Byte的值大于等于1GB
resultSize = df.format(size / (float) GB) + "GB";
} else if (size / MB >= 1) {
//如果当前Byte的值大于等于1MB
resultSize = df.format(size / (float) MB) + "MB";
} else if (size / KB >= 1) {
//如果当前Byte的值大于等于1KB
resultSize = df.format(size / (float) KB) + "KB";
} else {
resultSize = size + "B";
}
return resultSize;
}
public static void main(String[] args) throws Exception {
String ip = "192.168.10.90";
int port = 21;
String user = "root";
String pwd = "lgwy@2020";
String url = "/home/userfile/admin";
System.out.println(testFtp(ip,port,user,pwd));
System.out.println(getListFiles(ip,port,user,pwd,url));
}
}

View File

@ -0,0 +1,88 @@
package com.pancm.ftp;
import lombok.extern.slf4j.Slf4j;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import java.io.IOException;
@Slf4j
public final class SmartSshUtils {
public static boolean testSFTP(String hostName,
String username,
String password){
SSHClient ssh = new SSHClient();
SFTPClient sftpClient = null;
try {
//ssh.loadKnownHosts(); to skip host verification
ssh.addHostKeyVerifier(new PromiscuousVerifier());
ssh.connect(hostName);
ssh.authPassword(username, password);
sftpClient = ssh.newSFTPClient();
return true;
}catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static void downLoadFileBySsh(String hostName,
String username,
String password,
String srcFilePath,
String targetFilePath
) {
SSHClient ssh = new SSHClient();
SFTPClient sftpClient = null;
try {
//ssh.loadKnownHosts(); to skip host verification
ssh.addHostKeyVerifier(new PromiscuousVerifier());
ssh.connect(hostName);
ssh.authPassword(username, password);
sftpClient = ssh.newSFTPClient();
sftpClient.get(srcFilePath, targetFilePath);
//create a folder
// sftpClient.mkdir("/opt/app/testFolder");
//sftpClient.mkdirs("");创建多级文件夹
//sftpClient.rmdir("");重命名文件夹
//sftpClient.ls(""); //列出当前目录
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
if (null != sftpClient) {
try {
sftpClient.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
try {
ssh.disconnect();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
/**
* 静态工具类应该禁用构造方法
*/
private SmartSshUtils(){}
public static void main(String[] args) {
String hostName="192.168.9.80";
String username="root";
String password="Admin#12$34!";
String srcFilePath="/home/release/file";
String targetFilePath="D:\\d1";
SmartSshUtils.downLoadFileBySsh(hostName,username,password,srcFilePath,targetFilePath);
}
}

View File

@ -0,0 +1,9 @@
/**
* @Title: pancm_project
* @Description: ftp的工具类
* @Version:1.0.0
* @Since:jdk1.8
* @author pancm
* @date 2021/1/26
*/
package com.pancm.ftp;