SFTP获取文件夹内的文件名,文件大小,文件修改时间
使用SFTP协议获取服务上的指定路径的文件夹下的所有文件名,文件大小,文件的修改时时间等。
工具类
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .*;
import ;
import ;
/**
* @program: yc-bigdata-web
* @description: SFTP协议读取文件夹下的内容
* @author:
* @create: 2021-01-20 10:55
**/
public class SFTPUtil {
private ChannelSftp sftp;
private Session session;
private String username;
private String password;
private String privateKey;
private String host;
private int port;
public SFTPUtil(String username, String password, String host, int port) {
= username;
= password;
= host;
= port;
}
public SFTPUtil(String username, String host, int port, String privateKey) {
= username;
= host;
= port;
= privateKey;
}
public SFTPUtil(){}
public ChannelSftp getChannelSftp() {
return ;
}
public void login(){
try {
JSch jsch = new JSch();
if (privateKey != null) {
(privateKey);// 设置私钥
}
session = (username, host, port);
if (password != null) {
(password);
}
Properties config = new Properties();
("StrictHostKeyChecking", "no");
(config);
();
Channel channel = ("sftp");
();
sftp = (ChannelSftp) channel;
} catch (JSchException e) {
();
}
}
public void logout(){
if (sftp != null) {
if (()) {
();
}
}
if (session != null) {
if (()) {
();
}
}
}
public ArrayList<String> listFiles(String dir) throws SftpException{
ArrayList<String> files = new ArrayList<String>();
(dir);
Vector<String> lss = ("*");
for (int i = 0; i < (); i++) {
Object obj = (i);
if (obj instanceof ) {
LsEntry entry = (LsEntry) obj;
if (true && !().isDir()) {
(());
}
if (true && ().isDir()) {
if (!().equals(".") && !().equals("..")) {
(());
}
}
}
}
return files;
}
public void download(String fileName, String savedir) throws SftpException, IOException{
InputStream is = (fileName);
FileOutputStream out = new FileOutputStream(new File(savedir + "\\" +fileName));
(is,out);
();
();
}
public void delete(String fileName) throws SftpException {
(fileName);
}
public long getFileSize(String srcSftpFilePath) {
long fileSize;//文件大于等于0则存在
try {
SftpATTRS sftpATTRS = (srcSftpFilePath);
fileSize = ();
} catch (Exception e) {
fileSize = -1;//获取文件大小异常
if (().toLowerCase().equals("no such file")) {
fileSize = -2;//文件不存在
}
}
return fileSize;
}
public String getLastModifiedTime(String srcSftpFilePath)
try {
SftpATTRS sftpATTRS = (srcSftpFilePath);
int mTime = ();
Date lastModified = new Date(mTime * 1000L);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = (lastModified);
return time;
} catch (Exception e) {
();
}
return null;
}
}
方法的调用
SFTPUtil sftp = new SFTPUtil(username, pwd, ip, port);
();
ArrayList<String> fileList = null;
try {
fileList = (path);
for (int i = 0; i < (); i++) {
HashMap<String, Object> fileMessageMap = new HashMap<>();
String fileName = (i);
//获取文件大小
long size = (path + fileName);
String fileSize = (size);
//获取文件后缀名
String fileDOC = ((".") + 1);
//获取文件修改时间
String lastModifiedTime = (path + fileName);
("fileName", fileName);
("fileSize", fileSize);
("fileDOC",fileDOC);
("lastModifiedTime",lastModifiedTime);
(fileMessageMap);
}
();
} catch (SftpException e) {
();
}
return list;
}
return null;