package com.chinacreator.process.util.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SFtpUtil {
	
	private Log logger = LogFactory.getLog(FtpUtil.class);   
	
	private ChannelSftp ftpClient = null;
	
	private Session sshSession = null;
	
	private String errorcode="0000";
	private String errorinfo="OK";

	
	private ChannelSftp connectFTP(String host, String user, String pwd, int port){
		try {
			JSch jsch = new JSch();
 
			//获取sshSession  账号-ip-端口
			sshSession =jsch.getSession(user, host,port);
			//添加密码
			sshSession.setPassword(pwd);
			Properties sshConfig = new Properties();
			//严格主机密钥检查
			sshConfig.put("StrictHostKeyChecking", "no");
			
			sshSession.setConfig(sshConfig);
			//开启sshSession链接
			sshSession.connect();
			//获取sftp通道
			Channel channel = sshSession.openChannel("sftp");
			//开启
			channel.connect();
			ftpClient = (ChannelSftp) channel;
			
			return ftpClient;
		} catch (Exception e) {
			e.printStackTrace();
			
			logger.info("登录ftp服务器 " + host + " 失败,连接超时！");   
		}
		return null;
	}
	
	
	/**  
	 * 关闭连接  
	 */   
	private void closeConnect() {   
		try {   
			if(ftpClient!=null && ftpClient.isConnected()){
				ftpClient.disconnect();
			}
			
			if(sshSession!=null && sshSession.isConnected()){
				sshSession.disconnect();
			}
		} catch (Exception e) {   
			e.printStackTrace();   
		}   
	} 
	
	
		/**
	    * 上传对账文件
	    * 
	    * @param url
	    * @param user
	    * @param pwd
	    * @param port
	    * @param ftppath
	    * @param filepath
	    * @param filenames
	    * @return
	    */
	   public boolean UpBillFiles(String url,String user,String pwd,String port,String ftppath,String filepath,String filenames){
		   try {
			   //链接FTP
			   int p = Integer.parseInt(port);
			   ChannelSftp client = this.connectFTP(url, user, pwd, p);
			   if(client == null){
				   errorcode = "0101";
				   errorinfo = "FTP服务器 " + url + "连接失败";
				   return false;
			   }
			   
			   
			   //进入对应目录
			   if(ftppath!=null && !"".equals(ftppath.trim())){
				   try {
					   client.cd(ftppath);
				   } catch (Exception e) {
					   e.printStackTrace();
					   
					   logger.info("FTP服务器 " + url + "目录"+ftppath+"进入失败");   
					   errorcode = "0201";
					   errorinfo = "进入FTP服务器" + url + "目录"+ftppath+"失败";
					   return false;
				   }
			   }
			   
			   //对账文件保存路径
			   if(filepath == null || "".equals(filepath.trim()) || filenames == null || "".equals(filenames.trim())){
				   logger.info("文件目录或文件名异常");   
				   errorcode = "0301";
				   errorinfo = "文件目录或文件名异常";
				   return false;
			   }
			   
			   //上传文件
			   String [] fnames = filenames.split(",");
			   for(String fname :fnames){
				   File billfile = new File(filepath+fname);
				   if(!billfile.exists()){
					   logger.info("文件"+filepath+fname+"不存在");   
					   errorcode = "0302";
					   errorinfo = "文件"+filepath+fname+"不存在";
					   return false;
				   }
				   
				   try {
					   FileInputStream input=new FileInputStream(billfile);
					   client.put(input, fname);
				   } catch (Exception e) {
					   e.printStackTrace();
					   
					   logger.info("上传文件"+filepath+fname+"失败");   
					   errorcode = "0303";
					   errorinfo = "上传文件"+filepath+fname+"失败";
					   return false;
				   }
			   }
			   
			   //关闭ftp
			   this.closeConnect();
			   
			   return true;
		   } catch (Exception e) {
			   e.printStackTrace();
		   }
		   
		   return false;
	   }


		public String getErrorcode() {
			return errorcode;
		}


		public void setErrorcode(String errorcode) {
			this.errorcode = errorcode;
		}


		public String getErrorinfo() {
			return errorinfo;
		}


		public void setErrorinfo(String errorinfo) {
			this.errorinfo = errorinfo;
		}
	   
	   
}
