sftp文件传输

1.sftp

1.1 client

lib:jsch-0.1.52.jar,注意jsch版本与jdk的兼容问题,具体兼容对应关系待研究

package com.eplusing.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

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

public class SftpClient {
	private static final Logger LOGGER = Logger.getLogger(SftpClient.class);
	private String ip = "";
	private int port = 22;
	private String user = "";
	private String pwd = "";
	private ChannelSftp sftpClient = null;
	private String privateKeyPath = null;
	private String keyPwd = null;

	public static String CHARSET = "GBK";
	public static String BANK_SFTP_IP = "127.0.0.4";
	public static int BANK_SFTP_PORT = 22;
	public static String BANK_SFTP_USER = "sftpusername";
	public static String BANK_SFTP_PWD = "sftppassword";

	public static void main(String[] args) {
		SftpClient sftpClient = new SftpClient(BANK_SFTP_IP, BANK_SFTP_PORT);
		sftpClient.setUser(BANK_SFTP_USER);
		sftpClient.setPwd(BANK_SFTP_PWD);
		String remotePath = "./";
		String remoteFileName = "remoteFile.txt";

		String rspData = null;
		try {
			rspData = sftpClient.download(remotePath, remoteFileName);
		} catch (Exception e) {
			LOGGER.error("the exception file:" + remoteFileName, e);
		} finally {
			sftpClient.disconnect();
		}

		LOGGER.info(remoteFileName + " content:" + rspData);
	}

	public SftpClient(String ip, int port) {
		this.ip = ip;
		this.port = port;
	}

	public String download(String directory, String tFile) throws JSchException {
		if (null == sftpClient) {
			sftpClient = connect();
		}

		if (null == sftpClient) {
			throw new JSchException("connect sftp server exception");
		}

		StringBuffer sb = new StringBuffer();
		try {
			sftpClient.cd(directory);
			InputStream ins = sftpClient.get(tFile);
			BufferedReader br = new BufferedReader(new InputStreamReader(ins, CHARSET));
			String temp = null;
			while ((temp = br.readLine()) != null) {
				sb.append(temp);
			}
		} catch (Exception e) {
			LOGGER.error("下载文件异常", e);
		} finally {
			disconnect();
		}
		return sb.toString();
	}

	/**
	 * 按指定规则获取文件名称列表
	 * */
	public List<LsEntry> pullRptFiles(String remoteDir, String fileType) {
		SftpClient sftpClient = new SftpClient(BANK_SFTP_IP, BANK_SFTP_PORT);
		sftpClient.setUser(BANK_SFTP_USER);
		sftpClient.setPwd(BANK_SFTP_PWD);
		String latestMTime = "2018-11-18 23:34:34";
		try {
			sftpClient.connect();
			ChannelSftp client = sftpClient.getSftpClient();
			/**
			 * 目标文件名称:CAM52XX.<shortID>.<datestamp>.<extension>
			 * 目标文件名称:XMLSTXX.<shortID>.<datestamp>.<extension>
			 * */
			// 定义远程文件选择器
			final Pattern fileTypeMatch = Pattern.compile("^" + fileType + "XX.*(.txt|.TXT)$");
			final List<LsEntry> rptFiles = new ArrayList<LsEntry>();
			final String index = latestMTime;
			;
			LsEntrySelector selector = new LsEntrySelector() {
				@Override
				public int select(LsEntry entry) {
					Matcher mtc = fileTypeMatch.matcher(entry.getFilename());
					SftpATTRS attrs = entry.getAttrs();
					boolean isMatch = mtc.find() && !attrs.isDir() && !attrs.isLink();
					boolean isNewFile = index.compareTo(String.valueOf(attrs.getMTime())) < 0;
					if (isMatch && isNewFile) {
						rptFiles.add(entry);
					}
					return CONTINUE;
				}
			};

			client.ls(remoteDir, selector);

			LOGGER.info("增量文件数量:" + rptFiles.size());
			return rptFiles;
		} catch (SftpException e) {
			LOGGER.error("获取文件列表异常:", e);
		} catch (JSchException e) {
			LOGGER.error("获取文件列表异常:", e);
		} finally {
			sftpClient.disconnect();
		}

		return null;
	}

	public void download(String directory, String downloadFile, String saveFile) throws JSchException {
		if (null == sftpClient) {
			sftpClient = connect();
		}
		if (null == sftpClient) {
			throw new JSchException("connect sftp server exception");
		}
		try {
			sftpClient.cd(directory);
			File file = new File(saveFile);
			sftpClient.get(downloadFile, new FileOutputStream(file));
		} catch (Exception e) {
			LOGGER.error("", e);
		} finally {
			disconnect();
		}
	}

	public ChannelSftp connect() throws JSchException {
		if (null == user || user.length() == 0) {
			return null;
		}
		if ((null == pwd || pwd.length() == 0) && null == privateKeyPath) {
			return null;
		}

		JSch jsch = new JSch();
		Session session = null;

		if (null != privateKeyPath) {
			// 设置密钥和密码 支持密钥的方式登陆,只需在jsch.getSession之前设置一下密钥的相关信息就可以了
			try {
				if (null != keyPwd) {
					jsch.addIdentity(privateKeyPath, keyPwd);
				} else {
					jsch.addIdentity(privateKeyPath);
				}
			} catch (JSchException e) {
				LOGGER.error("设置证书异常", e);
				throw e;
			}
		}

		// 连接服务器,采用默认22端口
		session = jsch.getSession(user, ip, port);

		if (null != pwd && pwd.length() != 0) {
			session.setPassword(pwd);
		}

		// 如果服务器连接不上,则抛出异常
		if (session == null) {
			throw new JSchException("session is null");
		}

		// 设置第一次登陆的时候提示,可选值:(ask | yes | no)
		session.setConfig("StrictHostKeyChecking", "no");
		// 设置登陆超时时间
		session.connect(60 * 1000);
		sftpClient = (ChannelSftp) session.openChannel("sftp");
		sftpClient.connect(60 * 1000);

		return sftpClient;
	}

	/**
	 * Disconnect with server
	 */
	public void disconnect() {
		if (this.sftpClient != null) {
			if (this.sftpClient.isConnected()) {
				this.sftpClient.disconnect();
				this.sftpClient.exit();
			} else if (this.sftpClient.isClosed()) {
				LOGGER.info("sftp server connection is already closed ");
			}
			this.sftpClient = null;
		}
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	// 默认22端口
	public void setPort(int port) {
		if (port < 1) {
			this.port = 22;
		}
		this.port = port;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public void setPrivateKeyPath(String privateKeyPath) {
		this.privateKeyPath = privateKeyPath;
	}

	public void setKeyPwd(String keyPwd) {
		this.keyPwd = keyPwd;
	}

	public ChannelSftp getSftpClient() {
		return sftpClient;
	}
}

1.2 server

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值