1. 程式人生 > >Java通過sftp從目標主機下載檔案後讀取入庫,經過處理後推送到目標主機

Java通過sftp從目標主機下載檔案後讀取入庫,經過處理後推送到目標主機

在FileZilla 中: 

- 用sftp協議連線Server ,需要用port 22 
- 用ftp協議連線Server, 用port21

如果你的目標主機port是22,則是用sftp方式傳送,下載檔案,見下圖,該主機埠為22

一、連線目標主機下載目標檔案到本方主機,讀取本方主機檔案入庫

/**
 * 從目標主機同步目標資料入表
 */
package cn.finedo.business.village.task;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


import javax.annotation.Resource;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import cn.finedo.business.village.domain.Housdeptno;
import cn.finedo.business.village.service.HousdeptnoService;
import cn.finedo.business.village.tool.ConfigManager;
import cn.finedo.common.date.DateUtil;
import cn.finedo.common.domain.ReturnValueDomain;
import cn.finedo.common.non.NonUtil;
import cn.finedo.fsdp.service.common.jdbc.JdbcTemplate;

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


@Component
public class SynHousdeptno {

private Logger logger = LogManager.getLogger();

@Autowired
private HousdeptnoService housdeptnoService;

@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;

//同步過來的檔案儲存目錄
private static String downloadpath = ConfigManager.getInstance().getString("downloadpath");

//遠端ftp配置,配置在properties配置檔案中,

private String ftpip = ConfigManager.getInstance().getString("ftpip");//目標主機ip
private int ftpport = 22;//埠
private String ftpuser = ConfigManager.getInstance().getString("ftpuser");//有效的進入目標主機讀取資料的使用者名稱
private String ftppasswd = ConfigManager.getInstance().getString("ftppasswd");//目標主機密碼
private String remotepath = ConfigManager.getInstance().getString("remotepath");//本方主機檔案儲存路徑


/**
* 初始化
*/
public void startcreate(){

}


    //業務很簡單,一 連線目標主機下載目標檔案到本方主機,二,讀取本方主機檔案入庫,具體入資料庫和從資料庫刪除方法沒黏上

public void housdeptno() {
//連線主機
connected();
//讀取檔案入庫
logger.info("開始讀取檔案");
try {
readFile( downloadpath + File.separator + DateUtil.getNowTime("yyyyMMdd")+"_qywggx.txt",  "utf-8");
} catch (IOException e) {
logger.info("讀取檔案出錯",e);
}
    }

/**
* SFTP下載txt檔案
*/
public void connected(){
ChannelSftp sftp = null;  
   Channel channel = null;  
   Session sshSession = null;  
   try {  
   //連線主機
   JSch jsch = new JSch();  
            jsch.getSession(ftpuser, ftpip, ftpport);  
            sshSession = jsch.getSession(ftpuser, ftpip, ftpport);  
            sshSession.setPassword(ftppasswd);  
            Properties sshConfig = new Properties();  
            sshConfig.put("StrictHostKeyChecking", "no");  
            sshSession.setConfig(sshConfig);  
            sshSession.connect();  
            logger.info("Session connected!");  
            channel = sshSession.openChannel("sftp");  
            channel.connect();  
            logger.info("Channel connected!");  
            sftp = (ChannelSftp) channel;
            logger.info("連線上集中化主機");
            
           //下載檔案
       try{
         logger.info("下載的檔案為:"+remotepath + File.separator + DateUtil.getNowTime("yyyyMM") +File.separator + DateUtil.getNowTime("yyyyMMdd")+"_qywggx.txt");
         sftp.get(remotepath + File.separator + DateUtil.getNowTime("yyyyMM") +File.separator + DateUtil.getNowTime("yyyyMMdd")+"_qywggx.txt", downloadpath);
         logger.info("下載檔案成功");
         
       }catch (Exception e){
         logger.info("下載檔案出錯:",e);
       }


        } catch (Exception e) {
        logger.error("連線集中化主機異常",e);
        e.printStackTrace();  
        } finally {  
        //斷開連線
            closeChannel(sftp);  
            closeChannel(channel);  
            closeSession(sshSession);
        }
}

//讀資料入庫,具體業務,具體對待

public void readFile(String sourceFilePath, String encode) throws IOException{
 File file = new File(sourceFilePath);
 if(!file.exists()){    
     logger.info("檔案不存在");
     return;
 }   
 BufferedReader br = new   BufferedReader(new InputStreamReader(new FileInputStream(file), encode));
 System.out.println("找到檔案");
 String sLine = null;  
 List<Housdeptno> list = new ArrayList<Housdeptno>();
 
 while((sLine = br.readLine()) != null){
 sLine = sLine.trim();
 System.out.println("sline:"+sLine);
 String[] ary = sLine.split("\\$");
 Housdeptno housdeptno = new Housdeptno();
 housdeptno.setQybm(ary[0]);
 housdeptno.setParentqybm(ary[1]);
 housdeptno.setQymc(ary[2]);
 housdeptno.setQylx(ary[3]);

 list.add(housdeptno);
if(NonUtil.isNon(list)){
logger.info("未接收到資料");
br.close();
return;
}
if(list.size() >= 1000){
ReturnValueDomain<String> ret = housdeptnoService.add(list);
if("區域資訊新增失敗".equals(ret.getResultdesc())){
logger.info("當天入表資料出錯");


                                               //當天入庫的資料出錯,則把當天資料全部刪除,不同步


housdeptnoService.delete("T");
br.close();
return;
}
list.clear();
}
 }
 //讀到最後不足1000的入表
 ReturnValueDomain<String> ret = housdeptnoService.add(list);
if("區域資訊新增失敗".equals(ret.getResultdesc())){
logger.info("當天入表資料出錯");
housdeptnoService.delete("T");
br.close();
return;
}


                 //當天資料入表都成功,則把昨天資料清空


 housdeptnoService.delete("Y");
 br.close();
}



public void initconfig(){

}

private void closeChannel(Channel channel) {  
if (channel != null) {  
if (channel.isConnected()) {  
channel.disconnect();  
}  
}  
}  
  
private void closeSession(Session session) {  
if (session != null) {  
if (session.isConnected()) {  
session.disconnect();  
}  
}  
}  


}

二、本方主機生成檔案推送到目標主機



package cn.finedo.business.entering.task;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Properties;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;


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


import cn.finedo.business.entering.domain.EnteringInfo;
import cn.finedo.business.entering.service.EnteringSerivce;
import cn.finedo.business.entering.tool.ConfigManager;
import cn.finedo.common.code.GlobalCode;
import cn.finedo.common.date.DateUtil;
import cn.finedo.common.domain.ReturnValueDomain;
import cn.finedo.common.non.NonUtil;


public class CopyOfEntering {
private static Logger logger = LogManager.getLogger();


@Autowired
private EnteringSerivce enteringSerivce;

//生成同步社群資料錄入資訊檔案儲存目錄
private static String uploadpath = ConfigManager.getInstance().getString("uploadpath");


//遠端ftp配置
private String ftpip = ConfigManager.getInstance().getString("ftpip");
private int ftpport = 22;
private String ftpuser = ConfigManager.getInstance().getString("ftpuser");
private String ftppasswd = ConfigManager.getInstance().getString("ftppasswd");
private String remotepath = ConfigManager.getInstance().getString("remotepath");


/**
* 初始化
*/
public void startcreate(){

}

/***

* @author  wanghb
* @date 建立時間:2017-10-18 
* @description  FTP傳送txt檔案
* @parameter   
* @return
*/
public void transfertxt(){
ChannelSftp sftp = null;  
   Channel channel = null;  
   Session sshSession = null;  
   try {  
   JSch jsch = new JSch();  
            jsch.getSession(ftpuser, ftpip, ftpport);  
            sshSession = jsch.getSession(ftpuser, ftpip, ftpport);  
            sshSession.setPassword(ftppasswd);  
            Properties sshConfig = new Properties();  
            sshConfig.put("StrictHostKeyChecking", "no");  
            sshSession.setConfig(sshConfig);  
            sshSession.connect();  
            logger.info("Session connected!");  
            channel = sshSession.openChannel("sftp");  
            channel.connect();  
            logger.info("Channel connected!");  
            sftp = (ChannelSftp) channel;
            sftp.cd(remotepath);
        String filename = uploadpath+"/XQBG_"+DateUtil.getNowTime("yyyyMMdd")+".txt";
        if (filename == null || "".equals(filename)) {
          // 當前沒有要上傳的檔案
           logger.error("當前沒有要上傳的檔案");
     }else{
     File file=new File(filename);
     sftp.put(new FileInputStream(file), file.getName());
     logger.info("檔案傳送成功");
     }
        
        } catch (Exception e) {
        logger.error("連線ftp上傳檔案異常",e);
        e.printStackTrace();  
        } finally {  
            closeChannel(sftp);  
            closeChannel(channel);  
            closeSession(sshSession);
           
        }
}


/***

* @author  wanghb 
* @date 建立時間:2017-10-18  
* @description  生成txt檔案
* @parameter   
* @return
*/
public void createtxt(){
BufferedWriter writer = null;
boolean needtransfer = false;
try{
ReturnValueDomain<List<EnteringInfo>> ret = enteringSerivce.queryEnteringInfo();
if(ret.getResultcode().equals(GlobalCode.SUCCESS)){
List<EnteringInfo> list = ret.getObject();
if(NonUtil.isNotNon(list)){
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(uploadpath + File.separator + ("XQBG_"+DateUtil.getNowTime("yyyyMMdd")+".txt"))));
writer.write("地市編碼|地市名稱|區縣編碼|區縣名稱|區域編碼|區域名稱|網格編碼|網格名稱|小區編碼|小區名稱|樓棟編碼|樓棟名稱|門牌號編碼|門牌號名稱|移動寬頻是否覆蓋"
+ "|是否入住|寬頻運營商|資料維護人員|資料維護人員工號|維護時間|住戶聯絡人|戶主聯絡方式|寬頻型別|寬頻頻寬|寬頻賬號|寬頻到期時間");
for(EnteringInfo enteringInfo : list){
writer.write("\r\n");
writer.write(enteringInfo.getCitycode()+"|"+enteringInfo.getCityname()+"|"+enteringInfo.getCountycode()
+"|"+enteringInfo.getCountyname()+"|"+enteringInfo.getDepartcode()+"|"+enteringInfo.getDepartname()
+"|"+enteringInfo.getGridcode()+"|"+enteringInfo.getGridname()+"|"+enteringInfo.getHouscode()
+"|"+enteringInfo.getHousname()+"|"+enteringInfo.getBuildcode()+"|"+enteringInfo.getBuildname()
+"|"+enteringInfo.getRoomcode()+"|"+enteringInfo.getRoomname()
+"|"+enteringInfo.getIscover()+"|"+enteringInfo.getIscheckin()+"|"+enteringInfo.getYxs()
+"|"+enteringInfo.getOptrname()+"|"+enteringInfo.getOptrid()+"|"+enteringInfo.getOptdate()
+"|"+enteringInfo.getLinkname()+"|"+enteringInfo.getLinkphone()+"|"+enteringInfo.getKdtype()
+"|"+enteringInfo.getKdspeed()+"|"+enteringInfo.getKdno()+"|"+enteringInfo.getExpdate());
}
needtransfer = true;
}
}
writer.flush();

if(needtransfer){
transfertxt();
}

}catch (Exception e) {
logger.error("社群資料錄入資訊生成同步檔案異常", e);
e.printStackTrace();
}finally{
if(writer != null)
try {
writer.close();
} catch (IOException e) {
logger.error("關閉生成檔案異常",e);
e.printStackTrace();
}
writer = null;
}

}




private  void closeChannel(Channel channel) {  
if (channel != null) {  
if (channel.isConnected()) {  
channel.disconnect();  
}  
}  
}  
  
private void closeSession(Session session) {  
if (session != null) {  
if (session.isConnected()) {  
session.disconnect();  
}  
}  
}  
}