首先导入依赖
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency>
然后工具类
package mystudy.sftp; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.Vector; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.ChannelSftp.LsEntry; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpATTRS; public class SftpUtil { private ChannelSftp sftp; private Session session; private String host; private int port; private String username; private String password; private Exception exception; public SftpUtil() { } public SftpUtil(String host, int port, String username, String password) { this.host = host; this.port = port; this.username = username; this.password = password; checkInitParams(); } private void checkInitParams() { if (host == null || "".equals(host)) { throw new RuntimeException("paramsters are not initialized! or is blank"); } } public boolean connect() { exception = null; checkInitParams(); boolean ret = false; try { JSch jsch = new JSch(); session = jsch.getSession(username, host, port); session.setPassword(password); Properties config = new Properties(); config.put("StrictHostKeyChecking","no"); session.setConfig(config); session.connect(15000); Channel channel = session.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; ret = true; } catch(Exception e) { this.exception = e; } return ret; } public boolean connect(String host, int port, String username, String password) { exception = null; checkInitParams(); boolean ret = false; try { JSch jsch = new JSch(); session = jsch.getSession(username, host, port); session.setPassword(password); Properties config = new Properties(); config.put("StrictHostKeyChecking","no"); session.setConfig(config); session.connect(15000); Channel channel = session.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; ret = true; } catch(Exception e) { this.exception = e; } return ret; } public void disConnect() { if (sftp != null && sftp.isConnected()) { sftp.disconnect(); } if (session != null && session.isConnected()) { session.disconnect(); } } private void checkConnect() { if (sftp == null) { throw new RuntimeException("the sftp is null"); } } public boolean directoryIsExist(String directory) { exception = null; checkConnect(); boolean ret = false; try { SftpATTRS sftpAttrs = sftp.lstat(directory); return sftpAttrs.isDir(); } catch(Exception e) { this.exception = e; } return ret; } public InputStream readFile(String fileFullPath) { exception = null; checkConnect(); try { return sftp.get(fileFullPath); } catch(Exception e) { this.exception = e; } return null; } public List<String> listFiles(String directory) { exception = null; List<String> fileNameList = new ArrayList<String>(); try { sftp.cd(directory); Vector<?> fileVector = sftp.ls(directory); for (Object obj: fileVector) { ChannelSftp.LsEntry lsEntry = (LsEntry) obj; String fileName = lsEntry.getFilename(); fileNameList.add(fileName); } } catch(Exception e) { this.exception = e; } return fileNameList; } public boolean fileIsExist(String directory, String fileName) { List<String> fileNameList = listFiles(directory); return fileNameList.contains(fileName); } public boolean deleteFile(String directory, String fileName) { exception = null; boolean ret = false; try { sftp.cd(directory); sftp.rm(fileName); ret = true; } catch(Exception e) { this.exception = e; } return ret; } public boolean downloadFile(String sftpFileFullPath, String saveFileFullPath) { exception = null; boolean ret = false; try { FileOutputStream fos = new FileOutputStream(saveFileFullPath); sftp.get(sftpFileFullPath, fos); fos.flush(); fos.close(); ret = true; } catch(Exception e) { this.exception = e; } return ret; } public boolean uploadFile(String directory, String fileName, InputStream input) { exception = null; boolean ret = false; if (!directoryIsExist(directory)) { return ret; } try { sftp.cd(directory); sftp.put(input, fileName); ret = true; } catch(Exception e) { this.exception = e; } return ret; } public boolean createDirectory(String directory) { exception = null; boolean ret = false; try { if (!"/".equals(directory)) { String[] dirArray = directory.split("/"); String path = ""; for (String dir: dirArray) { if (dir != null && !"".equals(dir)) { path += "/" + dir; if (!directoryIsExist(path)) { sftp.mkdir(path); sftp.cd(path); } sftp.cd(path); } } } ret = true; } catch(Exception e) { this.exception = e; } return ret; } public Exception getException() { return this.exception; } }