作者:QFWQF2010_899 | 来源:互联网 | 2023-06-06 21:34
需求:
需要获得某个sftp上的某些增量的zip文件,并将zip文件中的txt中的内容保存到数据库。
txt中文件的内容是以 | 隔开。
zip文件每天新增一个,以前保存到数据库的zip中的数据不能重复保存。
package com.tmnch.tmnchpinan.ftpUtil;import com.jcraft.jsch.*;
import com.tmnch.tmnchpinan.entity.policyCount;
import com.tmnch.tmnchpinan.entity.sftpNameHistoryList;
import com.tmnch.tmnchpinan.ftpUtil.ftpConstant.constant;
import com.tmnch.tmnchpinan.service.FtpService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.zip.*;/*** * Description:* 获取sftp某个路径下的所有zip文件 并获得每个zip文件中的内容* ** * @author thw* * @date 2020/9/23 15:15* */@Configuration
@EnableScheduling
public class ftpServices {private static Session sshSession;@Autowiredprivate FtpService ftpService;//首先批量跑历史的zip文件
/*@Test
*/@Scheduled(cron = "0/25 * * * * ?")//定时public void runHistoryZip() {ZipEntry zipEntry = null;String historyZip = "";historyZip = getHistoryZipName(historyZip); //取出历史已经写入的zip文件try {ChannelSftp sftp = sftpConnection(constant.ftp_host, constant.ftp_port, constant.ftp_userNmae, constant.ftp_password);Vector v = sftp.ls(constant.ftpPath); //路径下所有文件if (v.size() > 0) {Iterator it = v.iterator();while (it.hasNext()) { //遍历某路径下的所有文件ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) it.next();String fileNameZip = entry.getFilename();//路径下某个文件名称名称if (fileNameZip.contains(constant.fileType)) {//获取所有zip文件对象System.out.println(fileNameZip);/*** 该处需要做特殊处理,去除掉以前曾经已经跑过的zip文件*/if (historyZip.length() > 0) {if (historyZip.contains(fileNameZip)) {continue;}}InputStream in = sftp.get(constant.ftpPath + fileNameZip);//获取文件内容ZipInputStream zin = new ZipInputStream(in);while ((zipEntry = zin.getNextEntry()) != null) {//遍历该zip文件内容String line = "";String fileNmae = null;String[] fileNmaeLIst = null;String tableNmae = null;BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(zin, "UTF-8"));while ((line = bufferedReader.readLine()) != null) {//读取该zip内文件的内容fileNmae = zipEntry.getName();//;路径名称fileNmaeLIst = fileNmae.split(constant.speatorTable);tableNmae = fileNmaeLIst[2];String[] sort = null;switch (tableNmae) {case "ProDclause":sort = line.split(constant.speator);System.out.println(sort[4]);continue;case "ProDClauseKind":sort = line.split(constant.speator);System.out.println(sort[5]);continue;}}}//特殊处理,保存过得zip需要将文件名存入记录表ftpService.insrtGistoryZip(fileNameZip);in.close();}}}sftpClose(sftp);sessionClose();} catch (Exception e) {e.printStackTrace();}//用于保存数据
/*List result =ftpService.getFtpTest("R51130600005");
*/}/*** @param host* @param password* @param port* @param userName* @return 连接对象* @description连接sftp**/public static ChannelSftp sftpConnection(String host, int port, String userName, String password) throws Exception {JSch jsch = new JSch();ChannelSftp channelSftp;jsch.getSession(userName, host, port);sshSession = jsch.getSession(userName, host, port);sshSession.setPassword(password);Properties properties = new Properties();properties.put("StrictHostKeyChecking", "no");sshSession.setConfig(properties);sshSession.connect();Channel channel = sshSession.openChannel("sftp");channel.connect();channelSftp = (ChannelSftp) channel;return channelSftp;}/*** @param* @return* @description 退出Sftp服务器登录**/public static void sftpClose(ChannelSftp channelSftp) {if (channelSftp != null) {if (channelSftp.isConnected()) {channelSftp.disconnect();}}}/*** 关闭session*/public static void sessionClose() {if (sshSession != null) {if (sshSession.isConnected()) {sshSession.disconnect();sshSession = null;}}}/*** 列出目录下的文件** @param directory:要列出的目录* @param sftp* @return* @throws SftpException*/public Vector listFiles(String directory, ChannelSftp sftp) throws SftpException {return sftp.ls(directory);}public String getHistoryZipName(String historyZip) {List sftpNameHistoryList = ftpService.selectZiphistory(historyZip);if (sftpNameHistoryList.size() > 0) {for (sftpNameHistoryList sftp : sftpNameHistoryList) {historyZip = historyZip + constant.speatorTable + sftp.getName();}}return historyZip;}
}
constant.java
//生产需要修改public static final String ftpPath = "/bin/";//文件路径public static final String ftp_host = "192.1.1.1";public static final int ftp_port = 22;public static final String ftp_userNmae = "root";public static final String ftp_password = "root";public static final String speator = "[|]";//文件内容隔离方式public static final String speatorTable = "/";//文件名隔离方式public static final String fileType = ".zip";//文件格式//1.000040\Acc\ProCInsureAcc