SFTP 文件上传下载工具类
SFTPUtils.java
import com.jcraft.jsch.*; import com.jcraft.jsch.ChannelSftp.LsEntry; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; import java.util.Vector; @Component @Slf4j public class SFTPUtils { private SFTPUtils instance = null; private ChannelSftp sftp; public File downloadFtpFile(String ftpHost,String ftpUserName,String ftpPassword,int ftpPort,String ftpPath,String localPath, String fileName, String downName){ if(null==instance){ instance=new SFTPUtils(); } //获取SFTP连接 sftp = instance.connect(ftpHost, ftpPort, ftpUserName,ftpPassword); //SFTP上下载文件 return instance.download(ftpPath+fileName, localPath+downName); } /** * 连接sftp服务器 * * @param host 主机 * @param port 端口 * @param username 用户名 * @param password 密码 * @return */ public ChannelSftp connect(String host, int port, String username, String password) { ChannelSftp sftp = null; try { JSch jsch = new JSch(); jsch.getSession(username, host, port); Session sshSession = jsch.getSession(username, host, port); sshSession.setPassword(password); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); sshSession.setConfig(sshConfig); sshSession.connect(); log.info("SFTP Session connected."); Channel channel = sshSession.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; log.info("Connected to " + host); } catch (Exception e) { log.error(e.getMessage()); } return sftp; } /** * 上传文件 * * @param directory 上传的目录 * @param uploadFile 要上传的文件 */ public boolean upload(String directory, String uploadFile) { try { sftp.cd(directory); File file = new File(uploadFile); FileInputStream fileInputStream = new FileInputStream(file); sftp.put(fileInputStream, file.getName()); fileInputStream.close(); return true; } catch (Exception e) { log.error(e.getMessage()); return false; } } /** * 下载文件 * * @param directory 下载目录 * @param downloadFile 下载的文件 * @param saveFile 存在本地的路径 */ public File download(String directory, String downloadFile, String saveFile) { try { sftp.cd(directory); File file = new File(saveFile); FileOutputStream fileOutputStream = new FileOutputStream(file); sftp.get(downloadFile, fileOutputStream); fileOutputStream.close(); return file; } catch (Exception e) { log.error(e.getMessage()); return null; } } /** * 下载文件 * * @param downloadFilePath 下载的文件完整目录 * @param saveFile 存在本地的路径 */ public File download(String downloadFilePath, String saveFile) { try { int i = downloadFilePath.lastIndexOf('/'); if (i == -1) return null; sftp.cd(downloadFilePath.substring(0, i)); File file = new File(saveFile); FileOutputStream fileOutputStream = new FileOutputStream(file); sftp.get(downloadFilePath.substring(i + 1), fileOutputStream); fileOutputStream.close(); return file; } catch (Exception e) { log.error(e.getMessage()); return null; } } /** * 删除文件 * * @param directory 要删除文件所在目录 * @param deleteFile 要删除的文件 */ public void delete(String directory, String deleteFile) { try { sftp.cd(directory); sftp.rm(deleteFile); } catch (Exception e) { log.error(e.getMessage()); } } /** * 断开连接 */ public void disconnect() { try { sftp.getSession().disconnect(); } catch (JSchException e) { log.error(e.getMessage()); } sftp.quit(); sftp.disconnect(); } /** * 列出目录下的文件 * * @param directory 要列出的目录 * @throws SftpException */ @SuppressWarnings("unused") public Vector<LsEntry> listFiles(String directory) throws SftpException { return sftp.ls(directory); } }
*********************
交流即分享,分享才能进步!
不对之处,还请各位前辈多多指教。
by 星云
********************