본문 바로가기

Spring Boot

[SpringBoot] SFTP파일전송 구현

반응형

1. 다음 종속성을 pom.xml에 추가합니다.

<dependency>
  <groupId>org.springframework.integration</groupId>
  <artifactId>spring-integration-file</artifactId>
  <version>5.4.2</version>
</dependency>

 

2. SFTP 세션 팩토리를 설정하기 위한 구성 클래스 생성.

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.integration.file.remote.session.CachingSessionFactory;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;

@Configuration
public class SftpConfig {

  @Value("${sftp.host}")
  private String sftpHost;

  @Value("${sftp.port:22}")
  private int sftpPort;

  @Value("${sftp.user}")
  private String sftpUser;

  @Value("${sftp.privateKey}")
  private String sftpPrivateKey;

  @Value("${sftp.privateKeyPassphrase}")
  private String sftpPrivateKeyPassphrase;

  @Bean
  public SessionFactory<LsEntry> sftpSessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost(sftpHost);
    factory.setPort(sftpPort);
    factory.setUser(sftpUser);
    factory.setPrivateKey(sftpPrivateKey);
    factory.setPrivateKeyPassphrase(sftpPrivateKeyPassphrase);
    factory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<>(factory);
  }
}

 

3. 파일을 SFTP 서버에 업로드하기 위한 서비스 클래스 만들기.

import java.io.File;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.sftp.session.SftpRemoteFileTemplate;
import org.springframework.stereotype.Service;

@Service
public class SftpService {

  private final SftpRemoteFileTemplate template;

  @Autowired
  public SftpService(SessionFactory<LsEntry> sessionFactory) {
    this.template = new SftpRemoteFileTemplate(sessionFactory);
  }

  public void uploadFile(String filePath, String remoteDirectory) {
    File file = new File(filePath);
    template.execute(
        session -> {
          session.mkdir(remoteDirectory);
          session.rename(file.getAbsolutePath(), remoteDirectory + "/" + file.getName());
        });
   }
}

 

## SFTP 세션 팩토리없이 구현

import java.io.File;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
import org.springframework.integration.sftp.session.SftpRemoteFileTemplate;
import org.springframework.stereotype.Service;

@Service
public class SftpService {

  private final SftpRemoteFileTemplate template;

  public SftpService(
      @Value("${sftp.host}") String sftpHost,
      @Value("${sftp.port:22}") int sftpPort,
      @Value("${sftp.user}") String sftpUser,
      @Value("${sftp.privateKey}") String sftpPrivateKey,
      @Value("${sftp.privateKeyPassphrase}") String sftpPrivateKeyPassphrase) {

    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost(sftpHost);
    factory.setPort(sftpPort);
    factory.setUser(sftpUser);
    factory.setPrivateKey(sftpPrivateKey);
    factory.setPrivateKeyPassphrase(sftpPrivateKeyPassphrase);
    factory.setAllowUnknownKeys(true);

    this.template = new SftpRemoteFileTemplate(new CachingSessionFactory<>(factory));
  }

  public void uploadFile(String filePath, String remoteDirectory) {
    File file = new File(filePath);
    template.execute(
        session -> {
          session.mkdir(remoteDirectory);
          session.rename(file.getAbsolutePath(), remoteDirectory + "/" + file.getName());
        });
  }
}

application.properties 파일에서 다음 속성에 대한 값을 제공하거나 환경 변수로 제공해야 합니다.

  • sftp.host
  • sftp.port
  • sftp.user
  • sftp.privateKey
  • sftp.privateKeyPassphrase
반응형