在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 {
//连接主机