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

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Vector;




import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class SFTPUtil {
	
	
	
	public static Session connect(String host, Integer port, String user, String password) throws JSchException{
		Session session = null;
		try {
			JSch jsch = new JSch();
			if(port != null){
				session = jsch.getSession(user, host, port.intValue());
			}else{
				session = jsch.getSession(user, host);
			}
			session.setPassword(password);
			
			session.setConfig("StrictHostKeyChecking", "no");
			//30秒连接超�?
			session.connect(3000);
		} catch (JSchException e) {
			e.printStackTrace();
			System.out.println("SFTPUitl connection error");
			throw e;
		}
		return session;
	}
	
	public static void upload(String directory, String uploadFile, ChannelSftp sftp) throws Exception{
		
		File file = new File(uploadFile);
		if(file.exists()){
			
			try {
				Vector content = sftp.ls(directory);
				if(content == null){
					sftp.mkdir(directory);
					System.out.println("mkdir:" + directory);
				}
			} catch (SftpException e) {
				sftp.mkdir(directory);
			}
			//进入目标路径
			sftp.cd(directory);
			System.out.println("directory: " + directory);
			if(file.isFile()){
				InputStream ins = new FileInputStream(file);
				
				sftp.put(ins, new String(file.getName().getBytes(),"UTF-8"));
				
			}else{
				File[] files = file.listFiles();
				for (File file2 : files) {
					String dir = file2.getAbsolutePath();
					if(file2.isDirectory()){
						String str = dir.substring(dir.lastIndexOf(file2.separator));
						directory = directory + str;
					}
					System.out.println("directory is :" + directory);
					upload(directory,dir,sftp);
				}
			}
		}
	}
	
}

@@ -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 
@@ -1753,16 +1753,16 @@
 %09%09//
-30%E7%A7%92%E8%BF%9E%E6%8E%A5%E8%B6%85%EF%BF%BD?
+time out
 %0D%0A%09%09
@@ -2334,27 +2334,14 @@
 );%0D%0A
+
 %09%09%09%7D%0D%0A
-%09%09%09//%E8%BF%9B%E5%85%A5%E7%9B%AE%E6%A0%87%E8%B7%AF%E5%BE%84%0D%0A
 %09%09%09s

Update SFTPUtil.java