From 4dce16e9d370f706ff5f34f7e3d25a856cf369fc Mon Sep 17 00:00:00 2001 From: leebeomjun Date: Fri, 12 Apr 2024 15:47:38 +0900 Subject: [PATCH] =?UTF-8?q?=EC=86=8C=EB=82=98=ED=81=90=EB=B8=8C=20?= =?UTF-8?q?=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cokr/xit/fims/cmmn/ftp/FTPUtil.java | 181 +++++++++--------- 1 file changed, 92 insertions(+), 89 deletions(-) diff --git a/src/main/java/cokr/xit/fims/cmmn/ftp/FTPUtil.java b/src/main/java/cokr/xit/fims/cmmn/ftp/FTPUtil.java index e4cb1321..9d6d51a2 100644 --- a/src/main/java/cokr/xit/fims/cmmn/ftp/FTPUtil.java +++ b/src/main/java/cokr/xit/fims/cmmn/ftp/FTPUtil.java @@ -18,105 +18,108 @@ public class FTPUtil { f.mkdirs(); FTPClient client = null; - try { - - String thisSystemFileSeperator = File.separator; - String remoteSystemFileSeparator = rs.getOsType().equals("linux") ? "/" : "\\"; + String thisSystemFileSeperator = File.separator; - if(!thisSystemFileSeperator.equals(remoteSystemFileSeparator)) { - remoteWorkPath = remoteWorkPath.replaceAll( - Matcher.quoteReplacement(thisSystemFileSeperator), - Matcher.quoteReplacement(remoteSystemFileSeparator) - ); - } + String remoteSystemFileSeparator = rs.getOsType().equals("linux") ? "/" : "\\"; + if(!thisSystemFileSeperator.equals(remoteSystemFileSeparator)) { + remoteWorkPath = remoteWorkPath.replaceAll( + Matcher.quoteReplacement(thisSystemFileSeperator), + Matcher.quoteReplacement(remoteSystemFileSeparator) + ); + } - client = new FTPClient(); - client.setControlEncoding("UTF-8"); + client = new FTPClient(); + client.setControlEncoding("UTF-8"); + try { client.connect(rs.getIp(), Integer.parseInt(rs.getPort())); + } catch (Exception e) { + throw new RuntimeException("접속 오류."); + } - // 접속을 확인. - int resultCode = client.getReplyCode(); - if (!FTPReply.isPositiveCompletion(resultCode)) { - System.out.println("FTP server refused connection.!"); - throw new RuntimeException("접속 오류"); - } - - client.setSoTimeout(1000); - // 로그인을 한다. - if (!client.login(rs.getId(), rs.getPw())) { - System.out.println("Login Error!"); - throw new RuntimeException("로그인 오류"); - } - - - List remoteFiles = new ArrayList<>(); - List remoteDirectories = new ArrayList<>(); - - // FTP에서 파일 리스트와 디렉토리 정보를 취득한다. - if (!FTPUtil.getFileList(client, remoteSystemFileSeparator, remoteWorkPath, remoteFiles, remoteDirectories)) { - // 리스트 취득 실패시 프로그램을 종료한다. - System.out.println("File search Error!"); - throw new RuntimeException("오류 발생"); - } - - for (String remoteDirectory : remoteDirectories) { - System.out.println("디렉토리-"+remoteDirectory); - - String newFolderPath = ""; - if(thisSystemFileSeperator.equals(remoteSystemFileSeparator)) { - newFolderPath = downloadRoot + remoteDirectory; - } else { - newFolderPath = downloadRoot + remoteDirectory.replaceAll( - Matcher.quoteReplacement(remoteSystemFileSeparator), - Matcher.quoteReplacement(thisSystemFileSeperator) - ); - } - - File folder = new File(newFolderPath); - folder.mkdirs(); - } - - - for (String remoteFile : remoteFiles) { - System.out.println("파일-"+remoteFile); - - String tempFileOutputPath = ""; - - if(thisSystemFileSeperator.equals(remoteSystemFileSeparator)) { - tempFileOutputPath = downloadRoot + remoteFile; - } else { - tempFileOutputPath = downloadRoot + remoteFile.replaceAll( - Matcher.quoteReplacement(remoteSystemFileSeparator), - Matcher.quoteReplacement(thisSystemFileSeperator) - ); - } - - try (FileOutputStream fo = new FileOutputStream(tempFileOutputPath)){ - //FTPClient의 retrieveFile함수로 보내면 다운로드가 이루어 진다. - if (client.retrieveFile(remoteFile, fo)) { - System.out.println("Download - " + remoteFile); - } - } - - } + // 접속을 확인. + int resultCode = client.getReplyCode(); + if (!FTPReply.isPositiveCompletion(resultCode)) { + throw new RuntimeException("접속 오류 : FTP server refused connection."); + } - if(client.isConnected()) { - client.disconnect(); - } - } catch (Exception e) { - if(client.isConnected()) { - try { - client.disconnect(); - } catch (IOException e1) { - throw new RuntimeException("오류 발생"); - } - } + try { + client.setSoTimeout(1000); + } catch (Exception e) { + throw new RuntimeException("접속 오류."); + } - throw new RuntimeException("오류 발생"); + boolean loginResult = false; + try { + // 로그인을 한다. + loginResult = client.login(rs.getId(), rs.getPw()); + } catch (Exception e) { + throw new RuntimeException("로그인 오류 : Login Error"); } + if (!loginResult) { + throw new RuntimeException("로그인 오류 : Login Error"); + } + + List remoteFiles = new ArrayList<>(); + List remoteDirectories = new ArrayList<>(); + + // FTP에서 파일 리스트와 디렉토리 정보를 취득한다. + if (!FTPUtil.getFileList(client, remoteSystemFileSeparator, remoteWorkPath, remoteFiles, remoteDirectories)) { + // 리스트 취득 실패시 프로그램을 종료한다. + throw new RuntimeException("파일 조회 오류 발생 : File search Error"); + } + + for (String remoteDirectory : remoteDirectories) { + System.out.println("디렉토리-"+remoteDirectory); + + String newFolderPath = ""; + if(thisSystemFileSeperator.equals(remoteSystemFileSeparator)) { + newFolderPath = downloadRoot + remoteDirectory; + } else { + newFolderPath = downloadRoot + remoteDirectory.replaceAll( + Matcher.quoteReplacement(remoteSystemFileSeparator), + Matcher.quoteReplacement(thisSystemFileSeperator) + ); + } + + File folder = new File(newFolderPath); + folder.mkdirs(); + } + + + for (String remoteFile : remoteFiles) { + System.out.println("파일-"+remoteFile); + + String tempFileOutputPath = ""; + + if(thisSystemFileSeperator.equals(remoteSystemFileSeparator)) { + tempFileOutputPath = downloadRoot + remoteFile; + } else { + tempFileOutputPath = downloadRoot + remoteFile.replaceAll( + Matcher.quoteReplacement(remoteSystemFileSeparator), + Matcher.quoteReplacement(thisSystemFileSeperator) + ); + } + + + try (FileOutputStream fo = new FileOutputStream(tempFileOutputPath)){ + //FTPClient의 retrieveFile함수로 보내면 다운로드가 이루어 진다. + if (client.retrieveFile(remoteFile, fo)) { + System.out.println("Download - " + remoteFile); + } + } catch(Exception e) { + throw new RuntimeException("파일 다운로드 오류"); + } + + } + + try { + client.disconnect(); + } catch(Exception e) { + throw new RuntimeException("파일 다운로드 오류"); + } return true; }