package cn.ict.zyq.bestConf.cluster.Utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.commons.io.IOUtils;

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;
import com.jcraft.jsch.SftpProgressMonitor;



/**
 * ʹ��SFTP��Զ�����������ļ���
 * 
 * @author ���� <aofengblog@163.com>
 */
public class RemoteFileAccess {

	/**
	 * ʹ��SFTP��Զ�����������ļ���
	 * 
	 * @param connectInfo
	 *            SFTP������Ϣ
	 * @param localPath
	 *            �����ļ�·��
	 * @param remotePath
	 *            Զ���ļ�·��
	 */
	public void download(SFtpConnectInfo connectInfo, String localPath, final String remotePath) {
		JSch jsch = new JSch();
		Session session = null;
		ChannelSftp channel = null;
		OutputStream outs = null;
		try {
			session = jsch.getSession(connectInfo.getUsername(), connectInfo.getHost(), connectInfo.getPort());
			session.setPassword(connectInfo.getPassword());
			Properties props = new Properties();
			props.put("StrictHostKeyChecking", "no");
			session.setConfig(props);
			session.connect(5000); // ����
			channel = (ChannelSftp) session.openChannel("sftp");
			channel.connect(5000);
			outs = new BufferedOutputStream(new FileOutputStream(new File(localPath)));
			channel.get(remotePath, outs, new SftpProgressMonitor() {

				

				private long current = 0;

				@Override
				public void init(int op, String src, String dest, long max) {
					
				}

				@Override
				public void end() {
					//if (logger.isInfoEnabled()) {
						//logger.debug(String.format("download file:%s complete", remotePath));
					//}
				}

				@Override
				public boolean count(long count) {
					current += count;
					//progress.show(current);

					return true;
				}
			});
		} catch (JSchException e) {
			System.err.println(String.format("connect remote host[%s:%d] occurs error "+connectInfo.getHost()+
					+connectInfo.getPort()));
			e.printStackTrace();
		} catch (SftpException e) {
			System.err.println(String.format("get remote file:%s occurs error", remotePath));
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			System.err.println(String.format("can not find local file:%s", localPath));
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(outs);
			if (null != channel) {
				channel.disconnect();
			}
			if (null != session) {
				session.disconnect();
			}
		}
	}
	public static void main(String[] args){
		
	}
}
@@ -486,309 +486,40 @@
 r;%0A%0A
-%0A%0A/**%0A * %CA%B9%EF%BF%BD%EF%BF%BDSFTP%EF%BF%BD%EF%BF%BD%D4%B6%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%C4%BC%EF%BF%BD%EF%BF%BD%EF%BF%BD%0A * %0A * @author %EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD %3Caofengblog@163.com%3E%0A */%0Apublic class RemoteFileAccess %7B%0A%0A%09/**%0A%09 * %CA%B9%EF%BF%BD%EF%BF%BDSFTP%EF%BF%BD%EF%BF%BD%D4%B6%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%C4%BC%EF%BF%BD%EF%BF%BD%EF%BF%BD%0A%09 * %0A%09 * @param connectInfo%0A%09 *            SFTP%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%CF%A2%0A%09 * @param localPath%0A%09 *            %EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%C4%BC%EF%BF%BD%C2%B7%EF%BF%BD%EF%BF%BD%0A%09 * @param remotePath%0A%09 *            %D4%B6%EF%BF%BD%EF%BF%BD%EF%BF%BD%C4%BC%EF%BF%BD%C2%B7%EF%BF%BD%EF%BF%BD%0A%09 */
+public class RemoteFileAccess %7B%0A
 %0A%09pu
@@ -1027,15 +1027,8 @@
 0); 
-// %EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD
 %0A%09%09%09
@@ -1424,126 +1424,8 @@
 %09%09%09%09
-//if (logger.isInfoEnabled()) %7B%0A%09%09%09%09%09%09//logger.debug(String.format(%22download file:%25s complete%22, remotePath));%0A%09%09%09%09%09//%7D
 %0A%09%09%09
@@ -1507,39 +1507,8 @@
 unt;
-%0A%09%09%09%09%09//progress.show(current);
 %0A%0A%09%09
@@ -2217,8 +2217,9 @@
 %0A%09%09%0A%09%7D%0A%7D
+%0A

Update RemoteFileAccess.java