java链接FTP操作

public class SFTPTool {
private String host;
private int port;
private String username;
private String password;


private Session sshSession = null;
private ChannelSftp sftp = null;
private static SFTPTool instance = null;


private SFTPTool() {
}


private SFTPTool(String host, int port, String username, String password) {
super();
this.host = host;
this.port = port;
this.username = username;
this.password = password;
}


public static SFTPTool getInstance(String host, int port, String username, String password) {
if (instance == null) {
instance = new SFTPTool(host, port, username, password);
}
return instance;
}


public String getHost() {
return host;
}


public void setHost(String host) {
this.host = host;
}


public int getPort() {
return port;
}


public void setPort(int port) {
this.port = port;
}


public String getUsername() {
return username;
}


public void setUsername(String username) {
this.username = username;
}


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


/**
* 连接ftp
*/
public void connect() {
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
sshSession = jsch.getSession(username, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();


// channel init
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (Exception e) {
throw new RuntimeException("ftp connect exception ", e);
}
}

/**
* 修改文件名称
* @param directory
* @param origFile
* @param targetFile
*/
public void rename(String directory, String origFile, String targetFile) {
try {
sftp.cd(directory);
sftp.rename(origFile, targetFile);
} catch (SftpException e) {
throw new RuntimeException("ftp rename exception ", e);
}
}


/**
* 断开连接
*/
public void disconnect() {
if (sftp != null) {
if (sftp.isConnected()) {
sftp.disconnect();
}
sftp = null;
}


if (sshSession != null) {
if (sshSession.isConnected()) {
sshSession.disconnect();
}
sshSession = null;
}
}


/**
* 获得目录的所有文件

* @param directory
* @return
* @throws SftpException
*/
@SuppressWarnings("unchecked")
public Vector<LsEntry> listFiles(String directory) throws SftpException {
return sftp.ls(directory);
}


/**
* 下载文件
* @param directory 进入下载目录
* @param containFileName 匹配的文件名称
* @param localFilePath 本地文件路径
* @return
* @throws Exception
*/
public List<String> download(String directory, String containFileName, String localFilePath)
throws Exception {
List<String> filePaths = new ArrayList<String>();
if (null != directory) {
try {
sftp.cd(directory);
} catch (Exception e) {
throw new FileNotFoundException("cd directory failed! directory:" + directory);
}
}

for (LsEntry file : listFiles(sftp.pwd())) {
if (file.getFilename().indexOf(containFileName)>-1) {
sftp.get(file.getFilename(), localFilePath);
filePaths.add(localFilePath + file.getFilename());
}
}
return filePaths;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值