From c2b29cea4e413f0afb1ed31b87b1014af76c18f7 Mon Sep 17 00:00:00 2001 From: leebj Date: Mon, 23 Sep 2024 12:33:39 +0900 Subject: [PATCH] =?UTF-8?q?SFTP=20=EA=B4=80=EB=A0=A8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 +- .../xit/fims/rent/service/bean/LsctBean.java | 167 ++++-------------- 2 files changed, 33 insertions(+), 140 deletions(-) diff --git a/pom.xml b/pom.xml index edb845c5..bcb44010 100644 --- a/pom.xml +++ b/pom.xml @@ -89,11 +89,7 @@ 1.0.0-SNAPSHOT - - com.github.mwiede - jsch - 0.2.20 - + diff --git a/src/main/java/cokr/xit/fims/rent/service/bean/LsctBean.java b/src/main/java/cokr/xit/fims/rent/service/bean/LsctBean.java index a8e72569..167758d1 100644 --- a/src/main/java/cokr/xit/fims/rent/service/bean/LsctBean.java +++ b/src/main/java/cokr/xit/fims/rent/service/bean/LsctBean.java @@ -1,18 +1,13 @@ package cokr.xit.fims.rent.service.bean; import java.io.IOException; -import java.io.InputStream; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Component; -import com.jcraft.jsch.Channel; -import com.jcraft.jsch.ChannelSftp; -import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; -import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; import cokr.xit.base.crypto.bean.XitAria; @@ -21,6 +16,8 @@ import cokr.xit.base.file.FileInfo.DataHolder; import cokr.xit.base.file.FileInfo.Relation; import cokr.xit.base.file.service.bean.FileBean; import cokr.xit.base.file.web.FileInfoFactory; +import cokr.xit.fims.cmmn.ftp.RemoteSystemInfo; +import cokr.xit.fims.cmmn.ftp.SFTPUtil; import cokr.xit.fims.crdn.Crdn; import cokr.xit.fims.crdn.service.bean.CrdnBean; import cokr.xit.fims.payer.Payer; @@ -247,52 +244,23 @@ public class LsctBean extends AbstractBean { .set("TASK_SE_CD", main.string("MM_TASKGB")); DataObject storageServer = finSysFtpMapper.selectFtpInfo(ftpQuery); - ///////////////////////////////////////////////// - //SFTP접속 - Session session = null; - JSch jsch = new JSch(); - Channel channel = null; - ChannelSftp channelSftp = null; - //세션객체 생성 - try { - session = jsch.getSession("admin", "127.0.0.1", Integer.parseInt("22")); - - session.setPassword("pass"); - - //세션관련 설정정보 설정 - java.util.Properties config = new java.util.Properties(); - - //호스트 정보 검사하지 않는다. - config.put("StrictHostKeyChecking", "no"); - session.setConfig(config); - - session.setTimeout(10000); - session.connect(); //접속 - channel = session.openChannel("sftp"); //sftp 채널 접속 - channel.connect(); - } catch (NumberFormatException e) { - throw new RuntimeException("[F] FTP 접속 오류"); - } catch (JSchException e) { - throw new RuntimeException("[F] FTP 접속 오류"); - } - channelSftp = (ChannelSftp) channel; + //FTP 서버 접속 + RemoteSystemInfo rs = new RemoteSystemInfo(); + rs.setId(storageServer.string("REMOTE_ID")); + rs.setIp(storageServer.string("REMOTE_IP")); + rs.setPort(storageServer.string("REMOTE_PORT")); + rs.setPw(storageServer.string("REMOTE_PASSWORD")); + rs.setOsType(storageServer.string("REMOTE_OS")); + rs.setWorkPath(storageServer.string("REMOTE_WORK_PATH")); + SFTPUtil sftpUtil = new SFTPUtil(); + sftpUtil.connect(rs); - String remoteWorkPath = storageServer.string("REMOTE_WORK_PATH"); - try { - channelSftp.cd(remoteWorkPath); - } catch (SftpException e) { - throw new RuntimeException("[F] FTP 경로 이동 오류"); - } - - //접속완료 - /////////////////////////////////////////////////////////////////// boolean fileSearchEnd = false; int i = 1; while(!fileSearchEnd) { /////////////////////////////////////////////// // 단속ID로 단속 파일 다운로드 - String i_char = ""; switch (i) { @@ -303,38 +271,19 @@ public class LsctBean extends AbstractBean { } String remoteFileName = main.string("MM_CODE")+i_char+".JPG"; - String remoteFilePath = remoteWorkPath+remoteFileName; - - InputStream is; - try { - is = channelSftp.get(remoteFilePath); - } catch (SftpException e) { - throw new RuntimeException("[F] FTP 파일 읽기 오류"); - } + String remoteFilePath = storageServer.string("REMOTE_WORK_PATH")+remoteFileName; - if(is == null) { + byte[] bytesObj = sftpUtil.getBytesObj(remoteFilePath); + if(bytesObj == null) { fileSearchEnd = true; continue; } - byte[] bytesOfFile; - try { - bytesOfFile = is.readAllBytes(); - } catch (IOException e) { - throw new RuntimeException("[F] FTP 파일 읽기 오류"); - } - - try { - is.close(); - } catch (IOException e) { - throw new RuntimeException("[F] FTP 파일 읽기 오류"); - } - ///////////////////////////////////////////// // 단속 파일 등록 Relation rel = new FileInfo.Relation().setInfoType(Crdn.INF_TYPE).setInfoKey(crdn.getCrdnId()); String originalFileName = remoteFileName; - DataHolder dataHolder = new FileInfo.DataHolder(rel, originalFileName, bytesOfFile); + DataHolder dataHolder = new FileInfo.DataHolder(rel, originalFileName, bytesObj); List fileInfoList = new FileInfoFactory().createFileInfos(List.of(dataHolder)); fileBean.create(fileInfoList); @@ -343,10 +292,9 @@ public class LsctBean extends AbstractBean { fileSearchEnd = true; } } - ///////////////////////////// //sftp연결종료 - session.disconnect(); + sftpUtil.disconnect(); } else { @@ -548,8 +496,6 @@ public class LsctBean extends AbstractBean { throw new RuntimeException("[F] 단속 부가 대장 등록 작업에 실패하였습니다."); // 예외를 발생시켜서 DB Rollback } - - ///////////////////////////////////////////////// //FTP 서버 정보 조회 DataObject main = mainList.get(iLoop); @@ -558,52 +504,23 @@ public class LsctBean extends AbstractBean { .set("TASK_SE_CD", main.string("MM_TASKGB")); DataObject storageServer = finSysFtpMapper.selectFtpInfo(ftpQuery); - ///////////////////////////////////////////////// - //SFTP접속 - Session session = null; - JSch jsch = new JSch(); - Channel channel = null; - ChannelSftp channelSftp = null; - //세션객체 생성 - try { - session = jsch.getSession("admin", "127.0.0.1", Integer.parseInt("22")); - - session.setPassword("pass"); - - //세션관련 설정정보 설정 - java.util.Properties config = new java.util.Properties(); - - //호스트 정보 검사하지 않는다. - config.put("StrictHostKeyChecking", "no"); - session.setConfig(config); - - session.setTimeout(10000); - session.connect(); //접속 - channel = session.openChannel("sftp"); //sftp 채널 접속 - channel.connect(); - } catch (NumberFormatException e) { - throw new RuntimeException("[F] FTP 접속 오류"); - } catch (JSchException e) { - throw new RuntimeException("[F] FTP 접속 오류"); - } - channelSftp = (ChannelSftp) channel; + //FTP 서버 접속 + RemoteSystemInfo rs = new RemoteSystemInfo(); + rs.setId(storageServer.string("REMOTE_ID")); + rs.setIp(storageServer.string("REMOTE_IP")); + rs.setPort(storageServer.string("REMOTE_PORT")); + rs.setPw(storageServer.string("REMOTE_PASSWORD")); + rs.setOsType(storageServer.string("REMOTE_OS")); + rs.setWorkPath(storageServer.string("REMOTE_WORK_PATH")); + SFTPUtil sftpUtil = new SFTPUtil(); + sftpUtil.connect(rs); - String remoteWorkPath = storageServer.string("REMOTE_WORK_PATH"); - try { - channelSftp.cd(remoteWorkPath); - } catch (SftpException e) { - throw new RuntimeException("[F] FTP 경로 이동 오류"); - } - - //접속완료 - /////////////////////////////////////////////////////////////////// boolean fileSearchEnd = false; int i = 1; while(!fileSearchEnd) { /////////////////////////////////////////////// // 단속ID로 단속 파일 다운로드 - String i_char = ""; switch (i) { @@ -614,38 +531,19 @@ public class LsctBean extends AbstractBean { } String remoteFileName = main.string("MM_CODE")+i_char+".JPG"; - String remoteFilePath = remoteWorkPath+remoteFileName; + String remoteFilePath = storageServer.string("REMOTE_WORK_PATH")+remoteFileName; - InputStream is; - try { - is = channelSftp.get(remoteFilePath); - } catch (SftpException e) { - throw new RuntimeException("[F] FTP 파일 읽기 오류"); - } - - if(is == null) { + byte[] bytesObj = sftpUtil.getBytesObj(remoteFilePath); + if(bytesObj == null) { fileSearchEnd = true; continue; } - byte[] bytesOfFile; - try { - bytesOfFile = is.readAllBytes(); - } catch (IOException e) { - throw new RuntimeException("[F] FTP 파일 읽기 오류"); - } - - try { - is.close(); - } catch (IOException e) { - throw new RuntimeException("[F] FTP 파일 읽기 오류"); - } - ///////////////////////////////////////////// // 단속 파일 등록 Relation rel = new FileInfo.Relation().setInfoType(Crdn.INF_TYPE).setInfoKey(crdn.getCrdnId()); String originalFileName = remoteFileName; - DataHolder dataHolder = new FileInfo.DataHolder(rel, originalFileName, bytesOfFile); + DataHolder dataHolder = new FileInfo.DataHolder(rel, originalFileName, bytesObj); List fileInfoList = new FileInfoFactory().createFileInfos(List.of(dataHolder)); fileBean.create(fileInfoList); @@ -654,10 +552,9 @@ public class LsctBean extends AbstractBean { fileSearchEnd = true; } } - ///////////////////////////// //sftp연결종료 - session.disconnect(); + sftpUtil.disconnect(); } else {