Java SFtpUtil

3533 ワード

		
			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("    ");
	}
	
	
}