在写用自动任务对对账文件内容进行筛选,避免敏感信息泄露的过程中,报了一个很有意思的错误:
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[]) {
}
}