Skip to content

Commit ef5dc3d

Browse files
committed
add ftp
1 parent b6783b7 commit ef5dc3d

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.pancm.ftp;
2+
3+
import org.apache.commons.net.ftp.FTPClient;
4+
import org.apache.commons.net.ftp.FTPFile;
5+
import org.apache.commons.net.ftp.FTPReply;
6+
7+
import java.io.IOException;
8+
import java.text.DecimalFormat;
9+
import java.util.ArrayList;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
/**
15+
* @author pancm
16+
* @Title: leakproof-server
17+
* @Description: ftp帮助类
18+
* @Version:1.0.0
19+
* @Since:jdk1.8
20+
* @date 2021/8/18
21+
*/
22+
public class FtpHelper {
23+
24+
/**
25+
* 获取文件列表文件的属性
26+
*
27+
* @param ip
28+
* @param port
29+
* @param user
30+
* @param pwd
31+
* @param url
32+
* @return
33+
*/
34+
public static List<Map<String, String>> getListFiles(String ip, int port, String user, String pwd, String url) throws IOException {
35+
List<Map<String, String>> mapList = new ArrayList<>();
36+
FTPClient ftpClient = new FTPClient();
37+
ftpClient.connect(ip, port);
38+
ftpClient.login(user, pwd);
39+
FTPFile[] ftpFiles = ftpClient.listFiles(url);
40+
if(ftpFiles!=null && ftpFiles.length>0) {
41+
for (FTPFile ftpFile : ftpFiles) {
42+
Map<String, String> map = new HashMap<>();
43+
map.put("fileName",ftpFile.getName());
44+
map.put("fileSize",getSize(ftpFile.getSize()));
45+
map.put("fileTime",ftpFile.getTimestamp().getTime().toString());
46+
mapList.add(map);
47+
}
48+
}
49+
return mapList;
50+
}
51+
52+
53+
54+
private static boolean testFtp(String ip, int port, String user, String pwd) throws IOException {
55+
FTPClient ftpClient = new FTPClient();
56+
ftpClient.connect(ip, port);//连接ftp
57+
ftpClient.login(user, pwd);//登陆ftp
58+
return FTPReply.isPositiveCompletion(ftpClient.getReplyCode());
59+
}
60+
61+
62+
63+
64+
public static String getSize(long size) {
65+
//获取到的size为:1705230
66+
long GB = 1024 * 1024 * 1024;//定义GB的计算常量
67+
long MB = 1024 * 1024;//定义MB的计算常量
68+
long KB = 1024;//定义KB的计算常量
69+
DecimalFormat df = new DecimalFormat("0.00");//格式化小数
70+
String resultSize = "";
71+
if (size / GB >= 1) {
72+
//如果当前Byte的值大于等于1GB
73+
resultSize = df.format(size / (float) GB) + "GB";
74+
} else if (size / MB >= 1) {
75+
//如果当前Byte的值大于等于1MB
76+
resultSize = df.format(size / (float) MB) + "MB";
77+
} else if (size / KB >= 1) {
78+
//如果当前Byte的值大于等于1KB
79+
resultSize = df.format(size / (float) KB) + "KB";
80+
} else {
81+
resultSize = size + "B";
82+
}
83+
return resultSize;
84+
}
85+
86+
87+
public static void main(String[] args) throws Exception {
88+
String ip = "192.168.10.90";
89+
int port = 21;
90+
String user = "root";
91+
String pwd = "lgwy@2020";
92+
String url = "/home/userfile/admin";
93+
System.out.println(testFtp(ip,port,user,pwd));
94+
System.out.println(getListFiles(ip,port,user,pwd,url));
95+
}
96+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.pancm.ftp;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import net.schmizz.sshj.SSHClient;
5+
import net.schmizz.sshj.sftp.SFTPClient;
6+
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
7+
8+
import java.io.IOException;
9+
10+
11+
@Slf4j
12+
public final class SmartSshUtils {
13+
14+
public static boolean testSFTP(String hostName,
15+
String username,
16+
String password){
17+
SSHClient ssh = new SSHClient();
18+
SFTPClient sftpClient = null;
19+
try {
20+
//ssh.loadKnownHosts(); to skip host verification
21+
ssh.addHostKeyVerifier(new PromiscuousVerifier());
22+
ssh.connect(hostName);
23+
ssh.authPassword(username, password);
24+
sftpClient = ssh.newSFTPClient();
25+
return true;
26+
}catch (IOException e) {
27+
e.printStackTrace();
28+
}
29+
30+
return false;
31+
}
32+
33+
34+
public static void downLoadFileBySsh(String hostName,
35+
String username,
36+
String password,
37+
String srcFilePath,
38+
String targetFilePath
39+
) {
40+
SSHClient ssh = new SSHClient();
41+
SFTPClient sftpClient = null;
42+
try {
43+
//ssh.loadKnownHosts(); to skip host verification
44+
ssh.addHostKeyVerifier(new PromiscuousVerifier());
45+
ssh.connect(hostName);
46+
ssh.authPassword(username, password);
47+
sftpClient = ssh.newSFTPClient();
48+
sftpClient.get(srcFilePath, targetFilePath);
49+
//create a folder
50+
// sftpClient.mkdir("/opt/app/testFolder");
51+
//sftpClient.mkdirs("");创建多级文件夹
52+
//sftpClient.rmdir("");重命名文件夹
53+
//sftpClient.ls(""); //列出当前目录
54+
} catch (IOException e) {
55+
log.error(e.getMessage(), e);
56+
} finally {
57+
if (null != sftpClient) {
58+
try {
59+
sftpClient.close();
60+
} catch (IOException e) {
61+
log.error(e.getMessage(), e);
62+
}
63+
}
64+
try {
65+
ssh.disconnect();
66+
} catch (IOException e) {
67+
log.error(e.getMessage(), e);
68+
}
69+
}
70+
}
71+
72+
/**
73+
* 静态工具类应该禁用构造方法
74+
*/
75+
private SmartSshUtils(){}
76+
77+
78+
public static void main(String[] args) {
79+
String hostName="192.168.9.80";
80+
String username="root";
81+
String password="Admin#12$34!";
82+
String srcFilePath="/home/release/file";
83+
String targetFilePath="D:\\d1";
84+
85+
SmartSshUtils.downLoadFileBySsh(hostName,username,password,srcFilePath,targetFilePath);
86+
87+
}
88+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @Title: pancm_project
3+
* @Description: ftp的工具类
4+
* @Version:1.0.0
5+
* @Since:jdk1.8
6+
* @author pancm
7+
* @date 2021/1/26
8+
*/
9+
package com.pancm.ftp;

0 commit comments

Comments
 (0)