1bb525556b3f3a83ef69ae47a0bad5212fab0d4b.svn-base 9.71 KB
package com.thinkgem.jeesite.modules.reg.web.updata;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SftpFileLoad
{
  protected static Logger logger = LoggerFactory.getLogger(SftpFileLoad.class);

  public String sshSftp(String ip, String user, String psw, int port, String filepath, String filename)
    throws Exception
  {
    logger.info("********开始调用上报数据方法**********");
    String tempReturnValue = "";
    logger.info("开始用户名密码方式登陆");
    Session session = null;
    JSch jsch = new JSch();
    if (port <= 0)
    {
      session = jsch.getSession(user, ip);
    }
    else {
      session = jsch.getSession(user, ip, port);
    }

    if (session == null) {
      throw new Exception("session is null");
    }

    session.setPassword(psw);

    session.setConfig("StrictHostKeyChecking", "no");

    session.connect(30000);
    logger.info("开始连接");
    tempReturnValue = sftp(session, filepath, filename);

    logger.info("******返回结果" + tempReturnValue + "*****");

    logger.info("********结束调用上报数据方法**********");
    return tempReturnValue;
  }

  private String sftp(Session session, String unloadfilepath, String uploadFileName) throws Exception
  {
    Channel channel = null;
    String tempReturnValue = "";
    try
    {
      channel = session.openChannel("sftp");
      channel.connect(30000);
      ChannelSftp sftp = (ChannelSftp)channel;

      sftp.cd("/home/610700/gtmap_client/client_source");

      OutputStream outstream = sftp.put(uploadFileName);
      InputStream instream = new FileInputStream(new File(unloadfilepath));

      byte[] b = new byte[1024];
      int n;
      while ((n = instream.read(b)) != -1) {
        outstream.write(b, 0, n);
      }
      outstream.flush();
      outstream.close();
      instream.close();
      tempReturnValue = sftpDownloadFile(session, unloadfilepath, uploadFileName);
      logger.info(tempReturnValue);
    } catch (Exception e) {
      logger.info("********出错****", e);
      e.printStackTrace();
    } finally {
      session.disconnect();
      channel.disconnect();
    }
    return tempReturnValue;
  }
  
  
  public String sftpDownloadFile(String ip, String user, String psw, int port, String filepath, String filename)
		    throws Exception
		  {
		    logger.info("********开始调用上报数据方法**********");
		    String tempReturnValue = "";
		    logger.info("开始用户名密码方式登陆");
		    Session session = null;
		    JSch jsch = new JSch();
		    if (port <= 0)
		    {
		      session = jsch.getSession(user, ip);
		    }
		    else {
		      session = jsch.getSession(user, ip, port);
		    }

		    if (session == null) {
		      throw new Exception("session is null");
		    }

		    session.setPassword(psw);

		    session.setConfig("StrictHostKeyChecking", "no");

		    session.connect(30000);
		    logger.info("开始连接");
		    tempReturnValue = sftpDownloadFile(session, filepath, filename);

		    logger.info("******返回结果" + tempReturnValue + "*****");

		    logger.info("********结束调用上报数据方法**********");
		    return tempReturnValue;
		  }

  private String sftpDownloadFile(Session session, String unloadfilepath, String uploadFileName) throws Exception {
    logger.info("********开始报文下载方法**********");
    Channel channel = null;
    String tempReturnValue = "";
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try
    {
      channel = session.openChannel("sftp");
      channel.connect(50000);
      ChannelSftp sftp = (ChannelSftp)channel;

      sftp.cd("/home/610700/gtmap_client/client_response");

      String tempname = "";
      tempname = uploadFileName.replace("Biz", "Rep");

      Thread.sleep(1000L);
      for (int i = 0; i < 15; ++i) {
        try {
          logger.info("********下载查询报文开始,时间为:" + df.format(new Date()));
          Vector content = sftp.ls(tempname);
          logger.info("********找到文件名为:" + tempname + ",的文件了,找到时间为:" + df.format(new Date()));
          i = 16;
        }
        catch (Exception e) {
          logger.info("********下载查询报文结束,时间为:" + df.format(new Date()));

          logger.info("********下载循环等待出错,最终等待时间为:****" + (200 * (i + 1)) + ",县区名称:" + tempname, e);
          Thread.sleep(200 * (i + 1));
        }

      }

      String areacode = uploadFileName.substring(3, 9);
      OutputStream outstream = new FileOutputStream(new File("D:/Rep/" + areacode + "/" + tempname));
      logger.info("********开始读取返回报文中的内容,时间为:" + df.format(new Date()));
      InputStream instream = sftp.get(tempname);
      byte[] b = new byte[1024];
      int n;
      while ((n = instream.read(b)) != -1) {
        outstream.write(b, 0, n);
      }
      outstream.flush();
      outstream.close();
      instream.close();
      tempReturnValue = parserXml("D:/Rep/" + areacode + "/" + tempname);
    } catch (Exception e) {
      logger.info("********下载出错时间为:" + df.format(new Date()), e);
      e.printStackTrace();
      logger.info(e.getMessage());
    } finally {
      session.disconnect();
      channel.disconnect();
    }
    logger.info("********结束报文下载方法了,时间为:" + df.format(new Date()));
    return tempReturnValue;
  }

  public String parserXml(String fileName) throws Exception
  {
    String tempReturnValue = "";
    File inputXml = new File(fileName);
    SAXReader saxReader = new SAXReader();
    try {
      Document document = saxReader.read(inputXml);
      Element employees = document.getRootElement();
      for (Iterator i = employees.elementIterator(); i.hasNext(); ) {
        Element employee = (Element)i.next();
        if (employee.getName() != "ResponseInfo")
          continue;
        tempReturnValue = employee.getText();
        return tempReturnValue;
      }
    }
    catch (DocumentException e)
    {
      logger.info(e.getMessage());
    }
    return tempReturnValue;
  }

  public String sshSftpdwoload(String ip, String user, String psw, int port, String filepath, String filename)
    throws Exception
  {
    logger.info("********开始调用上报数据方法**********");
    String tempReturnValue = "";
    logger.info("开始用户名密码方式登陆");
    Session session = null;
    JSch jsch = new JSch();
    if (port <= 0)
    {
      session = jsch.getSession(user, ip);
    }
    else {
      session = jsch.getSession(user, ip, port);
    }

    if (session == null) {
      throw new Exception("session is null");
    }

    session.setPassword(psw);

    session.setConfig("StrictHostKeyChecking", "no");

    session.connect(30000);
    logger.info("开始连接");
    tempReturnValue = querysftpDownloadFile(session, filepath, filename);

    logger.info("******返回结果" + tempReturnValue + "*****");

    logger.info("********结束调用上报数据方法**********");
    return tempReturnValue;
  }

  private String querysftpDownloadFile(Session session, String unloadfilepath, String uploadFileName)
    throws Exception
  {
    logger.info("********开始报文下载方法**********");
    Channel channel = null;
    String tempReturnValue = "";
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try
    {
      channel = session.openChannel("sftp");
      channel.connect(50000);
      ChannelSftp sftp = (ChannelSftp)channel;
      sftp.cd("/home/610700/gtmap_client/client_response");
      String tempname = "";
      tempname = uploadFileName.replace("Biz", "Rep");

      Thread.sleep(1000L);
      for (int i = 0; i < 15; ++i) {
        try {
          logger.info("********下载查询报文开始,时间为:" + df.format(new Date()));
          Vector content = sftp.ls(tempname);
          logger.info("********找到文件名为:" + tempname + ",的文件了,找到时间为:" + df.format(new Date()));
          i = 16;
        }
        catch (Exception e) {
          logger.info("********下载查询报文结束,时间为:" + df.format(new Date()));
          logger.info("********下载循环等待出错,最终等待时间为:****" + (200 * (i + 1)) + ",县区名称:" + tempname, e);
          Thread.sleep(200 * (i + 1));
        }

      }

      String areacode = uploadFileName.substring(3, 9);
      OutputStream outstream = new FileOutputStream(new File("D:/Rep/" + areacode + "/" + tempname));
      logger.info("********开始读取返回报文中的内容,时间为:" + df.format(new Date()));
      InputStream instream = sftp.get(tempname);
      byte[] b = new byte[1024];
      int n;
      while ((n = instream.read(b)) != -1) {
        outstream.write(b, 0, n);
      }
      outstream.flush();
      outstream.close();
      instream.close();
      tempReturnValue = parserXml("D:/Rep/" + areacode + "/" + tempname);
    } catch (Exception e) {
      logger.info("********下载出错时间为:" + df.format(new Date()), e);
      e.printStackTrace();
      logger.info(e.getMessage());
    } finally {
      session.disconnect();
      channel.disconnect();
    }
    logger.info("********结束报文下载方法了,时间为:" + df.format(new Date()));
    return tempReturnValue;
  }
}