Java操作SFTP工具类:
package test.sftp; import com.jcraft.jsch.*; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Properties; import java.util.Vector; public class SFTPUtil { private Session session = null; private ChannelSftp channel = null; private int timeout = 60000; /** * 连接sftp服务器 */ public boolean connect(String ftpUsername, String ftpAddress, int ftpPort, String ftpPassword) { boolean isSuccess = false; if (channel != null) { System.out.println("通道不为空"); return false; } JSch jSch = new JSch(); try { session = jSch.getSession(ftpUsername, ftpAddress, ftpPort); session.setPassword(ftpPassword); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.setTimeout(timeout); session.connect(); System.out.println("Session连接成功"); channel = (ChannelSftp) session.openChannel("sftp"); channel.connect(); System.out.println("通道连接成功"); isSuccess = true; } catch (JSchException e) { e.printStackTrace(); } return isSuccess; } /** * 关闭连接 */ public void close() { if (channel != null && channel.isConnected()) { channel.disconnect(); } if (session != null && session.isConnected()) { session.disconnect(); } } /** * 文件上传 * 采用默认的传输模式:OVERWRITE * * @param src 输入流 * @param dst 上传路径 * @param fileName 上传文件名 * @throws SftpException */ public boolean upLoadFile(InputStream src, String dst, String fileName) throws SftpException { boolean isSuccess = false; try { if (createDir(dst)) { channel.put(src, fileName); isSuccess = true; } } catch (SftpException e) { e.printStackTrace(); } return isSuccess; } /** * 创建一个文件目录 * * @param createpath 路径 * @return */ public boolean createDir(String createpath) { boolean isSuccess = false; try { if (isDirExist(createpath)) { channel.cd(createpath); return true; } String pathArry[] = createpath.split("/"); StringBuffer filePath = new StringBuffer("/"); for (String path : pathArry) { if (path.equals("")) { continue; } filePath.append(path + "/"); if (isDirExist(filePath.toString())) { channel.cd(filePath.toString()); } else { channel.mkdir(filePath.toString()); channel.cd(filePath.toString()); } } channel.cd(createpath); isSuccess = true; } catch (SftpException e) { e.printStackTrace(); } return isSuccess; } /** * 判断目录是否存在 * * @param directory 路径 * @return */ public boolean isDirExist(String directory) { boolean isSuccess = false; try { SftpATTRS sftpATTRS = channel.lstat(directory); isSuccess = true; return sftpATTRS.isDir(); } catch (Exception e) { if (e.getMessage().toLowerCase().equals("no such file")) { isSuccess = false; } } return isSuccess; } /** * 重命名指定文件或目录 */ public boolean rename(String oldPath, String newPath) { boolean isSuccess = false; try { channel.rename(oldPath, newPath); isSuccess = true; } catch (SftpException e) { e.printStackTrace(); } return isSuccess; } /** * 列出指定目录下的所有文件和子目录。 */ public Vector ls(String path) { try { Vector vector = channel.ls(path); return vector; } catch (SftpException e) { e.printStackTrace(); } return null; } /** * 删除文件 * * @param directory linux服务器文件地址 * @param deleteFile 文件名称 */ public boolean deleteFile(String directory, String deleteFile) { boolean isSuccess = false; try { channel.cd(directory); channel.rm(deleteFile); isSuccess = true; } catch (SftpException e) { e.printStackTrace(); } return isSuccess; } /** * 下载文件 * * @param directory 下载目录 * @param downloadFile 下载的文件 * @param saveFile 下载到本地路径 */ public boolean download(String directory, String downloadFile, String saveFile) { boolean isSuccess = false; try { channel.cd(directory); File file = new File(saveFile); channel.get(downloadFile, new FileOutputStream(file)); isSuccess = true; } catch (SftpException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } return isSuccess; } }
朝菌不知晦朔,蟪蛄不知春秋