SFTP-上传下载工具类

来源:互联网 发布:python try 重试 编辑:程序博客网 时间:2024/05/18 03:47
package com.xxx.patchgen.utils;import java.io.File;import java.util.HashMap;import java.util.Map;import java.util.Properties;import com.jcraft.jsch.Channel;import com.jcraft.jsch.ChannelSftp;import com.jcraft.jsch.JSch;import com.jcraft.jsch.JSchException;import com.jcraft.jsch.Session;public class SFTPUtil {Session session = null;Channel channel = null;public static final String SFTP_REQ_HOST = "host";public static final String SFTP_REQ_PORT = "port";public static final String SFTP_REQ_USERNAME = "username";public static final String SFTP_REQ_PASSWORD = "password";public static final int SFTP_DEFAULT_PORT = 22;public static final String SFTP_REQ_LOC = "location";public static void downloadFile(String src, String dst,Map<String, String> sftpDetails) throws Exception {SFTPUtil sftpUtil = new SFTPUtil();ChannelSftp chSftp = sftpUtil.getChannel(sftpDetails, 160000);try {// 下载文件dst.replace("\\", "/");int lastFirst = dst.lastIndexOf("/");            String path = dst.substring(0, lastFirst);            File localPath = new File(path);            if (!localPath.exists()) {// 路径不存在则创建本地目录                localPath.mkdirs();            }chSftp.get(src, dst);} catch (Exception e) {throw e;} finally {chSftp.quit();sftpUtil.closeChannel();}}public static void uploadFile(String src, String dst,Map<String, String> sftpDetails) throws Exception {SFTPUtil sftpUtil = new SFTPUtil();ChannelSftp chSftp = sftpUtil.getChannel(sftpDetails, 60000);try {// 上传文件chSftp.put(src, dst);} catch (Exception e) {throw e;} finally {chSftp.quit();sftpUtil.closeChannel();}}/** * 根据ip,用户名及密码得到一个SFTP * channel对象,即ChannelSftp的实例对象,在应用程序中就可以使用该对象来调用SFTP的各种操作方法 *  * @param sftpDetails * @param timeout * @return * @throws JSchException */public ChannelSftp getChannel(Map<String, String> sftpDetails, int timeout)throws JSchException {String ftpHost = sftpDetails.get(SFTP_REQ_HOST);String port = sftpDetails.get(SFTP_REQ_PORT);String ftpUserName = sftpDetails.get(SFTP_REQ_USERNAME);String ftpPassword = sftpDetails.get(SFTP_REQ_PASSWORD);int ftpPort = SFTP_DEFAULT_PORT;if (port != null && !port.equals("")) {ftpPort = Integer.valueOf(port);}JSch jsch = new JSch(); // 创建JSch对象session = jsch.getSession(ftpUserName, ftpHost, ftpPort); // 根据用户名,主机ip,端口获取一个Session对象if (ftpPassword != null) {session.setPassword(ftpPassword); // 设置密码}Properties config = new Properties();config.put("StrictHostKeyChecking", "no");session.setConfig(config); // 为Session对象设置propertiessession.setTimeout(timeout); // 设置timeout时间session.connect(5000); // 通过Session建立链接channel = session.openChannel("sftp"); // 打开SFTP通道channel.connect(); // 建立SFTP通道的连接return (ChannelSftp) channel;}public void closeChannel() throws Exception {if (channel != null) {channel.disconnect();}if (session != null) {session.disconnect();}}public static void main(String[] arg) throws Exception {// 设置主机ip,端口,用户名,密码Map<String, String> sftpDetails = new HashMap<String, String>();sftpDetails.put(SFTP_REQ_HOST, "192.168.12.182");sftpDetails.put(SFTP_REQ_USERNAME, "abc");sftpDetails.put(SFTP_REQ_PASSWORD, "abc@1");sftpDetails.put(SFTP_REQ_PORT, "2222");// 测试文件下载String srcFilename = "/FlashFXP绿色版.rar";String dstFilename = "F:/ftp-test/FlashFXP绿色版.rar";downloadFile(srcFilename, dstFilename, sftpDetails);}}