public class SftpUtil {

private final static Log logger = LogFactory.getLog(SftpUtil.class); static Log log = LogFactory.getLog(SftpUtil.class); /* * 从SFTP服务器下载文件 * * @param ftpHost SFTP IP地址 * * @param ftpUserName SFTP 用户名 * * @param ftpPassword SFTP用户名密码 * * @param ftpPort SFTP端口 * * @param ftpPath SFTP服务器中文件所在路径 格式: ftptest/aa * * @param localPath 下载到本地的位置 格式:H:/download * * @param fileName 文件名称 */ public static void downloadSftpFile(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort, String ftpPath, String localPath, String fileName) throws JSchException { Session session = null; Channel channel = null; JSch jsch = new JSch(); session = jsch.getSession(ftpUserName, ftpHost, ftpPort); session.setPassword(ftpPassword); session.setTimeout(100000); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.setConfig("kex","diffie-hellman-group1-sha1"); session.connect(); channel = session.openChannel("sftp"); channel.connect(); ChannelSftp chSftp = (ChannelSftp) channel; // String ftpFilePath = ftpPath + File.separatorChar + fileName; // String localFilePath = localPath + File.separatorChar + fileName; String ftpFilePath = ftpPath + fileName; String localFilePath = localPath + fileName; log.info("ftp路径:"+ftpFilePath); log.info("本地路径:"+localFilePath); try { chSftp.get(ftpFilePath, localFilePath); } catch (Exception e) { e.printStackTrace(); logger.info("download error."); } finally { chSftp.quit(); channel.disconnect(); session.disconnect(); } } /* * 上传文件到SFTP服务器 * * @param ftpHost SFTP IP地址 * * @param ftpUserName SFTP 用户名 * * @param ftpPassword SFTP用户名密码 * * @param ftpPort SFTP端口 * * @param sftpPath sftp上传路径 * * @param localPath 本地文件所在路径 * * @param fileName 本地文件名称 */ public static void uploadSftpFile(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort, String sftpPath, String localPath, String fileName) throws JSchException { Session session = null; Channel channel = null; JSch jsch = new JSch(); session = jsch.getSession(ftpUserName, ftpHost, ftpPort); session.setPassword(ftpPassword); session.setTimeout(100000); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); channel = session.openChannel("sftp"); channel.connect(); ChannelSftp chSftp = (ChannelSftp) channel; String ftpFilePath = sftpPath + File.separatorChar + fileName; String localFilePath = localPath + File.separatorChar + fileName; try { chSftp.put(localFilePath, ftpFilePath); } catch (Exception e) { e.printStackTrace(); logger.info("download error."); } finally { chSftp.quit(); channel.disconnect(); session.disconnect(); } } /** * @Description SFTP文件下载测试 * @throws JSchException */ public static void testDownloadFile() throws JSchException { //SFTP服务地址 String ftpHost = "192.168.100.21"; //用户名 String ftpUserName = "wl"; //密码 String ftpPassword = "wl"; //端口 int ftpPort = 22; //SFTP服务器文件路径 String ftpPath = "\\20170614111901996000000000000001\\"; //SFTP服务器文件名 String fileName = "九江银行-风险评估一键报告模板.docx"; //下载到本地的存储路径 String localPath = "C:\\file\\IG\\attach\\svc\\20170807134837329000000000000014\\"; //下载文件 downloadSftpFile(ftpHost, ftpUserName, ftpPassword, ftpPort, ftpPath, localPath, fileName); } /** * @Description SFTP文件上传测试 * @throws JSchException */ public static void testUploadFile() throws JSchException { //SFTP服务地址 String ftpHost = "192.168.100.21"; //用户名 String ftpUserName = "wl"; //密码 String ftpPassword = "wl"; //端口 int ftpPort = 22; //SFTP服务器文件路径 String ftpPath = "\\20170614111901996000000000000001\\"; //SFTP服务器文件名 String fileName = "常熟新需求工作计划.xlsx"; //上传到SFTP存储路径 String localPath = "C:\\file\\IG\\attach\\svc\\20170807134837329000000000000014\\"; //上传文件 uploadSftpFile(ftpHost, ftpUserName, ftpPassword, ftpPort, ftpPath, localPath, fileName); } /** * @Descript SFTP服务器文件传输测试 * @param args * @throws JSchException */ public static void main(String[] args) throws JSchException { // testDownloadFile(); // testUploadFile(); }