Jsch报错:com.jcraft.jsch.JSchException: session is down

在写用自动任务对对账文件内容进行筛选,避免敏感信息泄露的过程中,报了一个很有意思的错误:

com.jcraft.jsch.JSchException: session is down……

利用jsch读取服务器文件,用流的形式过滤敏感信息,生成新的对账txt文件,采用的jsch是最新版本的包:

经查,sftp的访问路径要求权限必须是750或者是755,不能设置成777,我的访问路径是/home/tomcat/data,竟然都是777:

修改目录权限,连接成功,简单贴上读取服务器文件并筛选信息生成新对账文件的代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ccb.ipsp.tools.DocDirect;
import com.ccb.ipsp.tools.PropertyUtils;
import com.ccb.ipsp.tools.SerialNumberTool;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SshUtil {

	private static final Logger log = LoggerFactory.getLogger(SshUtil.class);

	public String ip = PropertyUtils.getValue("PUBLIC_LOGIN_IP");
	public Integer port = Integer.parseInt(PropertyUtils.getValue("PUBLIC_LOGIN_PORT"));
	public String username = PropertyUtils.getValue("PUBLIC_LOGIN_USER");
	public String password = PropertyUtils.getValue("PUBLIC_LOGIN_PASSWORD");
	public String schoolProjectId = PropertyUtils.getValue("SCHOOL_PROJECT_ID");

	/**
	 * 利用JSch包实现SFTP下载、上传文件
	 * 
	 * @param ip
	 *            主机IP
	 * @param user
	 *            主机登陆用户名
	 * @param psw
	 *            主机登陆密码
	 * @param port
	 *            主机ssh2登陆端口,如果取默认值,传-1
	 */
	public void sshSftp() throws Exception {
		Session session = null;
		Channel channel = null;
		JSch jsch = new JSch();
		if (port <= 0) {
			// 连接服务器,采用默认端口
			session = jsch.getSession(username, ip);
		} else {
			// 采用指定的端口连接服务器
			session = jsch.getSession(username, ip, port);
		}

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

		// 设置登陆主机的密码
		session.setPassword(password);// 设置密码

		// 设置第一次登陆的时候提示,可选值:(ask | yes | no)
		session.setConfig("StrictHostKeyChecking", "no");
		// 设置登陆超时时间
		session.connect(60000);

		try {
			// 创建sftp通信通道
			channel = session.openChannel("sftp");
			channel.connect(1000);
			ChannelSftp sftp = (ChannelSftp) channel;
			log.info("连接成功--");
			// 进入服务器指定的文件夹
			sftp.cd(PropertyUtils.getValue("PUBLIC_FILE_PATH"));

			/*
			 * // 列出服务器指定的文件列表 
			 * Vector v = sftp.ls("*.dat"); 
			 * for (int i = 0; i < v.size(); i++) {
			 *  String fileName = String.valueOf(v.get(i));
			 *  log.info("第" + i + "个对账文件是:" + fileName); }
			 */
			// LCS_A3011_PAYMENT_FLOW.dat为支付流水
			String path = PropertyUtils.getValue("PUBLIC_FILE_PATH") + "/LCS_A3011_PAYMENT_FLOW.dat";
			System.err.println("进入支付对账文件路径=====" + path);

			String txtPayName = new DocDirect().returnDoc() + File.separator + PropertyUtils.getValue("SCHOOL_PAY_NAME");

			InputStream intstream = sftp.get(path); // 字节流
			InputStreamReader isr = new InputStreamReader(intstream); // 字符流
			BufferedReader br = new BufferedReader(isr); // 缓冲流
			try {
				LineNumberReader reader = new LineNumberReader(br);
				String txt = null;
				while ((txt = reader.readLine()) != null) {
					if (txt.contains("213135003")) {
						System.out.println("====\n" + txt);
						appendMethod(txtPayName, txt);
					}
				}
				log.info("支付数据筛选完毕!");
				reader.close();
				br.close();

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}

			// LCS_A3011_ITEM_FLOW.dat为明细流水
			String pathList = PropertyUtils.getValue("PUBLIC_FILE_PATH") + "/LCS_A3011_ITEM_FLOW.dat";
			System.err.println("进入明细对账文件路径=====" + pathList);

			String txtPayNameList = new DocDirect().returnDoc() + File.separator + PropertyUtils.getValue("SCHOOL_PAY_LIST_NAME");
		
			InputStream intstreamList = sftp.get(pathList); // 字节流
			InputStreamReader isrList = new InputStreamReader(intstreamList); // 字符流
			BufferedReader brList = new BufferedReader(isrList); // 缓冲流
			try {
				LineNumberReader reader = new LineNumberReader(brList);
				String txt = null;
				while ((txt = reader.readLine()) != null) {
					if (txt.contains("213135003")) {
						System.out.println("明细-----\n" + txt);
						appendMethod(txtPayNameList, txt);
					}
				}
				log.info("明细数据筛选完毕!");
				reader.close();
				br.close();

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.disconnect();
			channel.disconnect();
		}
	}

	// 追加写入
	public static void appendMethod(String fileName, String content) {
		try {
			// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
			FileWriter writer = new FileWriter(fileName, true);
			writer.write(content + "\n");
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 对账文件筛选留存
	public void DownCheckFile() {
		SshUtil sshUtil = new SshUtil();
		try {
			sshUtil.sshSftp();
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.info(SerialNumberTool.ConcreteDate() + "日对账文件筛选留存成功");
	}

	public static void main(String args[]) {

	}

}

 

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,你遇到的问题是`Caused by: com.jcraft.jsch.JSchException: Session.connect: java.net.SocketException: Connection reset`。这个错误通常是由于网络连接问题引起的。可能有以下几个原因导致这个错误: 1. 服务器端口未打开或被防火墙阻止:请确保你的SFTP服务器端口已正确打开,并且没有被防火墙阻止。 2. 服务器地址错误:请确保你的SFTP服务器地址是正确的,可以尝试使用ping命令来测试服务器的连通性。 3. 服务器连接超时:如果服务器连接超时,可能是由于网络延迟或服务器负载过高导致的。你可以尝试增加连接超时时间或者优化服务器性能。 4. 客户端配置错误:请确保你的Java代码中的SFTP配置是正确的,包括服务器地址、端口、用户名、密码等。 以下是一个示例代码,演示了如何使用JSch库进行SFTP上传文件: ```java import com.jcraft.jsch.*; public class SftpExample { public static void main(String[] args) { String host = "sftp.example.com"; int port = 22; String username = "your-username"; String password = "your-password"; String localFilePath = "/path/to/local/file.txt"; String remoteFilePath = "/path/to/remote/file.txt"; try { JSch jsch = new JSch(); Session session = jsch.getSession(username, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); channel.connect(); channel.put(localFilePath, remoteFilePath); channel.disconnect(); session.disconnect(); } catch (JSchException | SftpException e) { e.printStackTrace(); } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值