본문 바로가기

낙서장/Spring

Spring 다른 서버에 파일 보내기(FTP)

서버(Band-End)에서 다른 서버로 파일을 보내라는 일이 생겨서 처음 받을 때는 물음표가 머릿속에 무한대로 나왔는데 검색을 해보니 Network Socket을 이용하는 방법이 있어서 알아보다가 Socket 같은 경우에는 보내는 프로젝트뿐만 아니라 받는 프로젝트에도 코딩 작업을 해야 하는 번거로움이 있어서 PASS 그래서 다른 방법을 찾다가 FTP 라이브러리를 이용하는 방법이 있었다. 이 방법은 보내는 프로젝트만 코드를 작성하면 되고 기존 FTP GUI 이용한 것처럼 받는 서버의 HOST, ID, PW 알면 쉽게 보낼 수 있었다. 

 

그러나.. FTP는 보안상으로 사용 못한다고 해서 이거 못 씀... 꽤 쓸만하다고 생각했는데. 하 ㅋㅋㅋㅋ

 

lib

<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
		<dependency>
			<groupId>commons-net</groupId>
			<artifactId>commons-net</artifactId>
			<version>3.6</version>
		</dependency>

source

public boolean ftpPdfUpload(FtpVO ftpInfoVO) throws Exception{
		FTPClient ftp = null;
		BufferedInputStream pdfInput = null;
		boolean flag = false;
		
		try {
			ftp = new FTPClient();
//			ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); console 출력
			ftp.connect(ftpInfoVO.getHostServerIp());
			
			if(!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
				ftp.disconnect();
				throw new Exception("Exception in connecting to FTP Server");
			}
			
			ftp.login(ftpInfoVO.getUserName(), ftpInfoVO.getPassowrd());
			ftp.setFileType(FTP.BINARY_FILE_TYPE); //Upload File Type Setting 
			ftp.enterLocalPassiveMode(); //Passive Mode
			
			File pdfFile = new File(ftpInfoVO.getPafFilePath()+ftpInfoVO.getPdfFileName());

			if(!pdfFile.exists())
				throw new FileNotFoundException(ftpInfoVO.getPdfFileName()+"이 존재하지 않습니다.");
			
			pdfInput = new BufferedInputStream(new FileInputStream(pdfFile));
			flag = ftp.storeFile("src/main/webapp/upfile/pdf/"+ftpInfoVO.getPdfFileName(), pdfInput);
			
		}finally {
			if(pdfInput != null) {
				pdfInput.close();
			}
			
			if(ftp != null) {
				ftp.logout();
				ftp.disconnect();
			}
		}
		
		return flag;
	}

'낙서장 > Spring' 카테고리의 다른 글

Spring PDF 파일 ResponseEntity로 보여주기  (0) 2021.09.23
Oracle DataBase PDF파일 값 넣기!!  (0) 2021.09.07
Spring Project Setting  (0) 2021.08.19
Spring @Scheduled  (0) 2021.08.12
Spring Ajax JSON Encoding  (0) 2021.08.12