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;

public class RemoteFileAccess {

	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() {
					
				}

				@Override
				public boolean count(long count) {
					current += count;

					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){
		
	}
}

@@ -1,8 +1,816 @@
+/**%0D%0A * Copyright (c) 2017 Institute of Computing Technology, Chinese Academy of Sciences, 2017 %0D%0A * Institute of Computing Technology, Chinese Academy of Sciences contributors. All rights reserved.%0D%0A * %0D%0A * Licensed under the Apache License, Version 2.0 (the %22License%22); you%0D%0A * may not use this file except in compliance with the License. You%0D%0A * may obtain a copy of the License at%0D%0A * %0D%0A * http://www.apache.org/licenses/LICENSE-2.0%0D%0A *%0D%0A * Unless required by applicable law or agreed to in writing, software%0D%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0D%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or%0D%0A * implied. See the License for the specific language governing%0D%0A * permissions and limitations under the License. See accompanying%0D%0A * LICENSE file.%0D%0A */%0D%0A
 package 

Update RemoteFileAccess.java