SFTP 관련 수정

main
이범준 2 months ago
parent 5b2453d71f
commit c2b29cea4e

@ -89,11 +89,7 @@
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.mwiede</groupId>
<artifactId>jsch</artifactId>
<version>0.2.20</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -1,18 +1,13 @@
package cokr.xit.fims.rent.service.bean; package cokr.xit.fims.rent.service.bean;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.List; import java.util.List;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.springframework.stereotype.Component; 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.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException; import com.jcraft.jsch.SftpException;
import cokr.xit.base.crypto.bean.XitAria; 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.FileInfo.Relation;
import cokr.xit.base.file.service.bean.FileBean; import cokr.xit.base.file.service.bean.FileBean;
import cokr.xit.base.file.web.FileInfoFactory; 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.Crdn;
import cokr.xit.fims.crdn.service.bean.CrdnBean; import cokr.xit.fims.crdn.service.bean.CrdnBean;
import cokr.xit.fims.payer.Payer; import cokr.xit.fims.payer.Payer;
@ -247,52 +244,23 @@ public class LsctBean extends AbstractBean {
.set("TASK_SE_CD", main.string("MM_TASKGB")); .set("TASK_SE_CD", main.string("MM_TASKGB"));
DataObject storageServer = finSysFtpMapper.selectFtpInfo(ftpQuery); DataObject storageServer = finSysFtpMapper.selectFtpInfo(ftpQuery);
///////////////////////////////////////////////// //FTP 서버 접속
//SFTP접속 RemoteSystemInfo rs = new RemoteSystemInfo();
Session session = null; rs.setId(storageServer.string("REMOTE_ID"));
JSch jsch = new JSch(); rs.setIp(storageServer.string("REMOTE_IP"));
Channel channel = null; rs.setPort(storageServer.string("REMOTE_PORT"));
ChannelSftp channelSftp = null; rs.setPw(storageServer.string("REMOTE_PASSWORD"));
//세션객체 생성 rs.setOsType(storageServer.string("REMOTE_OS"));
try { rs.setWorkPath(storageServer.string("REMOTE_WORK_PATH"));
session = jsch.getSession("admin", "127.0.0.1", Integer.parseInt("22")); SFTPUtil sftpUtil = new SFTPUtil();
sftpUtil.connect(rs);
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;
String remoteWorkPath = storageServer.string("REMOTE_WORK_PATH");
try {
channelSftp.cd(remoteWorkPath);
} catch (SftpException e) {
throw new RuntimeException("[F] FTP 경로 이동 오류");
}
//접속완료
///////////////////////////////////////////////////////////////////
boolean fileSearchEnd = false; boolean fileSearchEnd = false;
int i = 1; int i = 1;
while(!fileSearchEnd) { while(!fileSearchEnd) {
/////////////////////////////////////////////// ///////////////////////////////////////////////
// 단속ID로 단속 파일 다운로드 // 단속ID로 단속 파일 다운로드
String i_char = ""; String i_char = "";
switch (i) { switch (i) {
@ -303,38 +271,19 @@ public class LsctBean extends AbstractBean {
} }
String remoteFileName = main.string("MM_CODE")+i_char+".JPG"; 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; fileSearchEnd = true;
continue; 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()); Relation rel = new FileInfo.Relation().setInfoType(Crdn.INF_TYPE).setInfoKey(crdn.getCrdnId());
String originalFileName = remoteFileName; String originalFileName = remoteFileName;
DataHolder dataHolder = new FileInfo.DataHolder(rel, originalFileName, bytesOfFile); DataHolder dataHolder = new FileInfo.DataHolder(rel, originalFileName, bytesObj);
List<FileInfo> fileInfoList = new FileInfoFactory().createFileInfos(List.of(dataHolder)); List<FileInfo> fileInfoList = new FileInfoFactory().createFileInfos(List.of(dataHolder));
fileBean.create(fileInfoList); fileBean.create(fileInfoList);
@ -343,10 +292,9 @@ public class LsctBean extends AbstractBean {
fileSearchEnd = true; fileSearchEnd = true;
} }
} }
///////////////////////////// /////////////////////////////
//sftp연결종료 //sftp연결종료
session.disconnect(); sftpUtil.disconnect();
} else { } else {
@ -548,8 +496,6 @@ public class LsctBean extends AbstractBean {
throw new RuntimeException("[F] 단속 부가 대장 등록 작업에 실패하였습니다."); // 예외를 발생시켜서 DB Rollback throw new RuntimeException("[F] 단속 부가 대장 등록 작업에 실패하였습니다."); // 예외를 발생시켜서 DB Rollback
} }
///////////////////////////////////////////////// /////////////////////////////////////////////////
//FTP 서버 정보 조회 //FTP 서버 정보 조회
DataObject main = mainList.get(iLoop); DataObject main = mainList.get(iLoop);
@ -558,52 +504,23 @@ public class LsctBean extends AbstractBean {
.set("TASK_SE_CD", main.string("MM_TASKGB")); .set("TASK_SE_CD", main.string("MM_TASKGB"));
DataObject storageServer = finSysFtpMapper.selectFtpInfo(ftpQuery); DataObject storageServer = finSysFtpMapper.selectFtpInfo(ftpQuery);
///////////////////////////////////////////////// //FTP 서버 접속
//SFTP접속 RemoteSystemInfo rs = new RemoteSystemInfo();
Session session = null; rs.setId(storageServer.string("REMOTE_ID"));
JSch jsch = new JSch(); rs.setIp(storageServer.string("REMOTE_IP"));
Channel channel = null; rs.setPort(storageServer.string("REMOTE_PORT"));
ChannelSftp channelSftp = null; rs.setPw(storageServer.string("REMOTE_PASSWORD"));
//세션객체 생성 rs.setOsType(storageServer.string("REMOTE_OS"));
try { rs.setWorkPath(storageServer.string("REMOTE_WORK_PATH"));
session = jsch.getSession("admin", "127.0.0.1", Integer.parseInt("22")); SFTPUtil sftpUtil = new SFTPUtil();
sftpUtil.connect(rs);
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;
String remoteWorkPath = storageServer.string("REMOTE_WORK_PATH");
try {
channelSftp.cd(remoteWorkPath);
} catch (SftpException e) {
throw new RuntimeException("[F] FTP 경로 이동 오류");
}
//접속완료
///////////////////////////////////////////////////////////////////
boolean fileSearchEnd = false; boolean fileSearchEnd = false;
int i = 1; int i = 1;
while(!fileSearchEnd) { while(!fileSearchEnd) {
/////////////////////////////////////////////// ///////////////////////////////////////////////
// 단속ID로 단속 파일 다운로드 // 단속ID로 단속 파일 다운로드
String i_char = ""; String i_char = "";
switch (i) { switch (i) {
@ -614,38 +531,19 @@ public class LsctBean extends AbstractBean {
} }
String remoteFileName = main.string("MM_CODE")+i_char+".JPG"; String remoteFileName = main.string("MM_CODE")+i_char+".JPG";
String remoteFilePath = remoteWorkPath+remoteFileName; String remoteFilePath = storageServer.string("REMOTE_WORK_PATH")+remoteFileName;
InputStream is; byte[] bytesObj = sftpUtil.getBytesObj(remoteFilePath);
try { if(bytesObj == null) {
is = channelSftp.get(remoteFilePath);
} catch (SftpException e) {
throw new RuntimeException("[F] FTP 파일 읽기 오류");
}
if(is == null) {
fileSearchEnd = true; fileSearchEnd = true;
continue; 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()); Relation rel = new FileInfo.Relation().setInfoType(Crdn.INF_TYPE).setInfoKey(crdn.getCrdnId());
String originalFileName = remoteFileName; String originalFileName = remoteFileName;
DataHolder dataHolder = new FileInfo.DataHolder(rel, originalFileName, bytesOfFile); DataHolder dataHolder = new FileInfo.DataHolder(rel, originalFileName, bytesObj);
List<FileInfo> fileInfoList = new FileInfoFactory().createFileInfos(List.of(dataHolder)); List<FileInfo> fileInfoList = new FileInfoFactory().createFileInfos(List.of(dataHolder));
fileBean.create(fileInfoList); fileBean.create(fileInfoList);
@ -654,10 +552,9 @@ public class LsctBean extends AbstractBean {
fileSearchEnd = true; fileSearchEnd = true;
} }
} }
///////////////////////////// /////////////////////////////
//sftp연결종료 //sftp연결종료
session.disconnect(); sftpUtil.disconnect();
} else { } else {

Loading…
Cancel
Save