SFTP-上传下载工具类

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 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 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 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对象设置properties
		session.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 sftpDetails = new HashMap();
		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);
	}
}

你可能感兴趣的:(JAVA)