Java SFtpUtil

		
			org.objectweb.joram
			jftp
			1.60
		
package cn.fg.demo.util;

import java.io.FileNotFoundException;
import java.io.InputStream;
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;
import com.jcraft.jsch.SftpException;

public class SFtpUtil {
	private String username;
	private String password;
	private String host;
	private int port;
	private String privateKey;
	private Session session;
	private String Type = "sftp";
	private ChannelSftp sftp;
	/**
	 * 基于密码认证
	 * @param username
	 * @param password
	 * @param host
	 * @param port
	 */
	public SFtpUtil(String username, String password, String host, int port) {
		super();
		this.username = username;
		this.password = password;
		this.host = host;
		this.port = port;
	}
	
	/**
	 * 基于秘钥认证
	 * @param username
	 * @param host
	 * @param port
	 * @param privateKey
	 */
	public SFtpUtil(String username, String host, int port, String privateKey) {
		super();
		this.username = username;
		this.host = host;
		this.port = port;
		this.privateKey = privateKey;
	}
	
	public void login() throws JSchException{
		JSch jsch = new JSch();

			if (privateKey != null) {
				//设置私钥
				jsch.addIdentity(privateKey);
			}
			session = jsch.getSession(username,host,port);
			if (password != null) {
				session.setPassword(password);
			}
			session.setTimeout(20000);
			Properties config = new Properties();
			config.put("StrictHostKeyChecking", "no");
			//config.put("kex", "diffie-hellman-group1-sha1");
			session.setConfig(config);
			session.connect();
			
			Channel channel = session.openChannel(Type);
			channel.connect();
			sftp = (ChannelSftp)channel;
	}

	/**
	 * 关闭Server
	 */
	public void logout(){
		if (sftp !=null) {
			if (sftp.isConnected()) {
				sftp.disconnect();
			}
		}
		if (session != null) {
			if (session.isConnected()) {
				session.disconnect();
			}
		}
		
	}
	
	/**
	 * 上传,完整路径=basePath + directory
	 * @param basePath服务器基础路径
	 * @param directory上传到该目录
	 * @param sftpFileName sftp端文件名
	 * @param input
	 * @throws SftpException 
	 */
	public void upload(String basePath,String directory,String sftpFileName,
			InputStream input) throws SftpException{
		try {
			sftp.cd(basePath);
			sftp.cd(directory);
		} catch (Exception e) {
			//目录不存在,则创建文件夹
			String[] dirs = directory.split("/");
			String tempPath = basePath;
			for (String dir : dirs) {
				
				if (null == dir || "".equals(dir)) {
					continue;
				}
				tempPath+="/" + dir;
				try {
					sftp.cd(tempPath);
				} catch (SftpException e2) {
					sftp.mkdir(tempPath);
					sftp.cd(tempPath);
				}
			}
		}
		sftp.put(input,sftpFileName);
	}
	
	
	public static void main(String[] args) throws FileNotFoundException, SftpException, JSchException {
		
		SFtpUtil sftp = new SFtpUtil("sftp-test", "123456", "197.18.65.10", 22);
		sftp.login();
//		File file = new File("D:/85006_001.acpk");
//		FileInputStream is = new FileInputStream(file);
//		System.out.println("======upload=====前");
//		sftp.upload("/fg", "", "85006_001.acpk", is);
//		System.out.println("======upload=====后");
		sftp.logout();
		System.out.println("处理完成");
	}
	
	
}

 

你可能感兴趣的:(工具类)