인터넷 통신

FTP란 무엇일까?

jaycheol 2025. 3. 28. 15:38
반응형

FTP란 무엇인가요?

FTP(File Transfer Protocol)는 파일을 한 컴퓨터에서 다른 컴퓨터로 전송하는 방법입니다. 주로 네트워크 상에서 사용되며, 클라이언트(파일을 보내는 사람 또는 프로그램)와 서버(파일을 받는 사람 또는 프로그램) 간에 데이터를 전송할 때 사용됩니다.

기본 원리

  • FTP 서버: 파일을 저장하고 있는 컴퓨터입니다. FTP 서버는 파일을 클라이언트가 요청할 때 전송해 주는 역할을 합니다.
  • FTP 클라이언트: 파일을 보내거나 받을 수 있는 프로그램입니다. 사용자는 FTP 클라이언트를 통해 서버에 접속하고, 파일을 전송할 수 있습니다.

FTP 동작 방식

  1. FTP 서버에 연결: FTP 클라이언트는 서버의 IP 주소와 포트를 통해 서버에 연결합니다. 기본적으로 FTP는 21번 포트를 사용합니다.
  2. 로그인: 서버에 접속 후, 사용자는 로그인해야 합니다. 일반적으로 사용자 이름과 비밀번호가 필요합니다.
  3. 파일 전송: 로그인 후, 클라이언트는 서버에 있는 파일을 다운로드하거나, 서버로 파일을 업로드할 수 있습니다.

FTP 사용 예시

  • 파일 다운로드: 웹 서버에 저장된 이미지를 자신의 컴퓨터로 다운로드.
  • 파일 업로드: 자신의 컴퓨터에서 웹 서버로 HTML 파일이나 이미지 파일을 업로드.

자바에서 FTP 사용 방법

자바에서는 FTP 프로토콜을 사용하여 파일을 전송하는 프로그램을 만들 수 있습니다. 이를 위해 Apache Commons Net 라이브러리를 사용할 수 있습니다. 이 라이브러리는 FTP 클라이언트를 쉽게 구현할 수 있게 도와줍니다.

 

1. Apache Commons Net 라이브러리 추가

먼저, Maven을 사용하는 프로젝트라면 pom.xml에 Apache Commons Net 라이브러리를 추가합니다.

<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.8.0</version>
</dependency>

2. FTP 연결 및 파일 업로드/다운로드 예제

자바에서는 다음과 같이 FTP 서버에 연결하고 파일을 업로드하거나 다운로드할 수 있습니다.

import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FtpExample {

    public static void main(String[] args) {
        FTPClient ftpClient = new FTPClient();
        try {
            // 서버에 연결
            ftpClient.connect("ftp.example.com");
            ftpClient.login("username", "password");
            
            // 업로드할 파일 경로 설정
            File file = new File("path/to/local/file.txt");
            FileInputStream fis = new FileInputStream(file);

            // FTP 서버에 파일 업로드
            ftpClient.storeFile("remote/file.txt", fis);
            fis.close();
            
            System.out.println("파일 업로드 성공!");

            // 파일 다운로드 예시
            ftpClient.retrieveFile("remote/file.txt", new FileOutputStream("path/to/local/downloaded_file.txt"));
            System.out.println("파일 다운로드 성공!");

            ftpClient.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

스프링에서 FTP 사용 방법

스프링에서는 FTP를 활용하는 데 여러 가지 방법이 있지만, 가장 일반적으로 사용하는 방법은 **Spring Integration**을 활용하는 것입니다. Spring Integration은 FTP 서버와의 파일 전송을 간단하게 처리할 수 있게 도와주는 라이브러리입니다.

1. Spring Integration에서 FTP 사용하기

Spring Integration을 사용하면, FTP 서버와의 상호작용을 매우 쉽게 설정할 수 있습니다. 설정 방법은 다음과 같습니다.

Maven 의존성 추가

먼저, spring-integration-ftp 라이브러리를 의존성에 추가합니다.

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-ftp</artifactId>
    <version>5.5.0</version>
</dependency>
 
FTP 서버와 통신하는 간단한 설정 예시
@Configuration
@EnableIntegration
public class FtpConfig {

    @Bean
    public FtpInboundChannelAdapter ftpInboundChannelAdapter() {
        FtpInboundChannelAdapter adapter = new FtpInboundChannelAdapter();
        adapter.setRemoteDirectory("/remote/dir");
        adapter.setLocalDirectory(new File("path/to/local/dir"));
        adapter.setFilter(new SimplePatternFileListFilter("*.txt"));
        adapter.setSessionFactory(ftpSessionFactory());
        return adapter;
    }

    @Bean
    public SessionFactory<LsEntry> ftpSessionFactory() {
        DefaultFtpSessionFactory factory = new DefaultFtpSessionFactory();
        factory.setHost("ftp.example.com");
        factory.setUsername("username");
        factory.setPassword("password");
        return new CachingSessionFactory<>(factory);
    }
}

위 예시에서 FtpInboundChannelAdapter는 FTP 서버에서 파일을 다운로드하는 역할을 하며, ftpSessionFactory()는 FTP 서버와의 연결 정보를 설정합니다.

FTP의 활용 예시

  1. 파일 백업 시스템: 스프링 애플리케이션에서 일정 주기로 중요한 데이터를 FTP 서버에 자동으로 백업하는 시스템을 구현할 수 있습니다.
  2. 대용량 파일 전송: 기업 내에서 대용량 파일을 서버 간에 안전하고 효율적으로 전송하는 데 FTP를 사용할 수 있습니다.
반응형