|
|
|
@ -1,12 +1,10 @@
|
|
|
|
|
package org.springblade.ftpdemo.controller; |
|
|
|
|
|
|
|
|
|
import com.jcraft.jsch.*; |
|
|
|
|
import org.springblade.ftpdemo.util.Constant; |
|
|
|
|
|
|
|
|
|
import java.io.*; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.Iterator; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Vector; |
|
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class SSHRemoteCall { |
|
|
|
@ -84,7 +82,7 @@ public class SSHRemoteCall {
|
|
|
|
|
JSch jSch = new JSch(); |
|
|
|
|
try { |
|
|
|
|
// 获取到jSch的session, 根据用户名、主机ip、端口号获取一个Session对象
|
|
|
|
|
session = jSch.getSession(userName, ipAddress, 22); //012
|
|
|
|
|
session = jSch.getSession(userName, ipAddress, 22102); //22102
|
|
|
|
|
// 设置密码
|
|
|
|
|
session.setPassword(password); |
|
|
|
|
|
|
|
|
@ -201,7 +199,7 @@ public class SSHRemoteCall {
|
|
|
|
|
// 打开channelSftp
|
|
|
|
|
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); |
|
|
|
|
// 远程连接
|
|
|
|
|
channelSftp.connect(); |
|
|
|
|
channelSftp.connect(6000); |
|
|
|
|
// 创建一个文件名称问uploadFile的文件
|
|
|
|
|
File file = new File(uploadFile); |
|
|
|
|
// 将文件进行上传(sftp协议)
|
|
|
|
@ -269,7 +267,7 @@ public class SSHRemoteCall {
|
|
|
|
|
try { |
|
|
|
|
channelSftp = (ChannelSftp) session.openChannel("sftp"); |
|
|
|
|
// 远程连接
|
|
|
|
|
channelSftp.connect(); |
|
|
|
|
channelSftp.connect(6000); |
|
|
|
|
// 切换到主目录
|
|
|
|
|
// channelSftp.cd("/");
|
|
|
|
|
} catch (JSchException e) { |
|
|
|
@ -289,12 +287,11 @@ public class SSHRemoteCall {
|
|
|
|
|
channelSftp.cd(folder); |
|
|
|
|
} catch (SftpException ex) { |
|
|
|
|
throw new RuntimeException(ex); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
channelSftp.disconnect(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -352,44 +349,37 @@ public class SSHRemoteCall {
|
|
|
|
|
* @throws SftpException |
|
|
|
|
* @throws JSchException |
|
|
|
|
*/ |
|
|
|
|
public void listFiles(String directory) throws JSchException, SftpException { |
|
|
|
|
public List<String> listFiles(String directory) throws JSchException, SftpException { |
|
|
|
|
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); |
|
|
|
|
// 远程连接
|
|
|
|
|
channelSftp.connect(); |
|
|
|
|
// 显示目录信息
|
|
|
|
|
Vector ls = channelSftp.ls(directory); |
|
|
|
|
|
|
|
|
|
Iterator iterator = ls.iterator(); |
|
|
|
|
|
|
|
|
|
Map<String, File> fileMap = new HashMap<>(); |
|
|
|
|
Iterator iterator = ls.iterator(); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
ArrayList<String> list = new ArrayList<>(); |
|
|
|
|
while (iterator.hasNext()) { |
|
|
|
|
ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) iterator.next(); |
|
|
|
|
//文件名称
|
|
|
|
|
String fileName = file.getFilename(); |
|
|
|
|
System.out.println(fileName); |
|
|
|
|
//todo 这里可使用if或正则不下载一些文件
|
|
|
|
|
// InputStream inputStream = channelSftp.get(directory + fileName);
|
|
|
|
|
// if (inputStream != null) {
|
|
|
|
|
// File newFile = new File(fileName);
|
|
|
|
|
// //将InputStream转File
|
|
|
|
|
//// FileUtils.copyToFile(inputStream, newFile);
|
|
|
|
|
// fileMap.put(fileName, newFile);
|
|
|
|
|
// }
|
|
|
|
|
//移除上级目录和根目录:"." ".."
|
|
|
|
|
if (".".equals(fileName) || "..".equals(fileName)) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
list.add(fileName); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return list; |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} finally { |
|
|
|
|
// 切断连接
|
|
|
|
|
channelSftp.exit(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|