https://www.cnblogs.com/itmanxgl/p/fe5d33512609fe540eb08a76e3c4db3a.html
https://www.cnblogs.com/dongliyang/p/4173583.html
本Sftp工具类的API如下所示。
1)构造方法摘要
参数:
|
2)方法摘要
boolean |
changeDir(String pathName) 切换工作目录 |
boolean | changeToHomeDir() 切换到根目录 |
boolean | changeToParentDir() 切换到上一级目录 |
String | currentDir() 当前工作目录 |
boolean | delDir(String dirName) 删除文件夹 |
boolean | delFile(String fileName) 删除文件 |
boolean | downloadFile(String remotePath, String fileName,String localPath) 下载文件 |
boolean | exist(String name) 当前目录是否存在文件或文件夹 |
boolean | exist(String path, String name) 指定目录下,是否存在文件或文件夹 |
boolean | existDir(String name) 当前目录是否存在文件夹 |
boolean | existDir(String path, String name) 指定目录下,是否存在文件夹 |
boolean | existFile(String name) 当前目录是否存在文件 |
boolean | existFile(String path, String name) 指定目录下,是否存在文件 |
boolean | login() 登陆SFTP服务器 |
void | logout() 登出 |
String[] | ls() 当前目录下文件及文件夹名称列表 |
String[] | ls(String pathName) 指定目录下文件及文件夹名称列表 |
String[] | lsDirs() 当前目录下文件夹名称列表 |
String[] | lsDirs(String pathName) 指定目录下文件夹名称列表 |
String[] | lsFiles() 当前目录下文件名称列表 |
String[] | lsFiles(String pathName) 指定目录下文件名称列表 |
boolean |
makeDir(String dirName) 创建目录 |
boolean | uploadFile(String pathName,String fileName,InputStream input) 上传文件 |
boolean | uploadFile(String pathName, String fileName, String localFile) 上传文件 |
3)源代码
1 import java.io.File; 2 import java.io.InputStream; 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Properties; 6 import java.util.Vector; 7 import org.apache.log4j.Logger; 8 import com.jcraft.jsch.ChannelSftp; 9 import com.jcraft.jsch.JSch; 10 import com.jcraft.jsch.JSchException; 11 import com.jcraft.jsch.Session; 12 import com.jcraft.jsch.SftpException; 13 import com.jcraft.jsch.ChannelSftp.LsEntry; 14 15 /** 16 * SFTP(Secure File Transfer Protocol),安全文件传送协议。 17 * @version 1.0 2014/12/18 18 * @author dongliyang 19 */ 20 public class Sftp { 21 22 /** 日志记录器 */ 23 private Logger logger = Logger.getLogger(Sftp.class); 24 /** Session */ 25 private Session session = null; 26 /** Channel */ 27 private ChannelSftp channel = null; 28 /** SFTP服务器IP地址 */ 29 private String host; 30 /** SFTP服务器端口 */ 31 private int port; 32 /** 连接超时时间,单位毫秒 */ 33 private int timeout; 34 35 /** 用户名 */ 36 private String username; 37 /** 密码 */ 38 private String password; 39 40 /** 41 * SFTP 安全文件传送协议 42 * @param host SFTP服务器IP地址 43 * @param port SFTP服务器端口 44 * @param timeout 连接超时时间,单位毫秒 45 * @param username 用户名 46 * @param password 密码 47 */ 48 public Sftp(String host,int port,int timeout,String username,String password){ 49 this.host = host; 50 this.port = port; 51 this.timeout = timeout; 52 this.username = username; 53 this.password = password; 54 } 55 56 /** 57 * 登陆SFTP服务器 58 * @return boolean 59 */ 60 public boolean login() { 61 62 try { 63 JSch jsch = new JSch(); 64 session = jsch.getSession(username, host, port); 65 if(password != null){ 66 session.setPassword(password); 67 } 68 Properties config = new Properties(); 69 config.put("StrictHostKeyChecking", "no"); 70 session.setConfig(config); 71 session.setTimeout(timeout); 72 session.connect(); 73 logger.debug("sftp session connected"); 74 75 logger.debug("opening channel"); 76 channel = (ChannelSftp)session.openChannel("sftp"); 77 channel.connect(); 78 79 logger.debug("connected successfully"); 80 return true;