빈쿵바라기
좌충우돌 개발자의 기록
빈쿵바라기
전체 방문자
오늘
어제
  • 분류 전체보기 (53)
    • Programming (25)
      • JAVA (12)
      • Spring Boot (6)
      • JPA (7)
      • Python (0)
    • Database (12)
      • RDBMS (4)
      • NoSQL (7)
    • Server (11)
    • Elasticsearch (3)
    • ETC (2)

블로그 메뉴

    공지사항

    인기 글

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    빈쿵바라기

    좌충우돌 개발자의 기록

    Programming/JAVA

    [Apache VFS] SFTP 파일 업로드

    2023. 3. 8. 19:03

    공식 Document : Commons Virtual File System

     

    Apache Commons VFS – Commons Virtual File System

    Commons Virtual File System Commons VFS provides a single API for accessing various different file systems. It presents a uniform view of the files from various different sources, such as the files on local disk, on an HTTP server, or inside a Zip archive.

    commons.apache.org

     

    1. Dependency 추가

    <!-- Apache Commons VFS -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-vfs2</artifactId>
        <version>2.9.0</version>
    </dependency>
    
    <dependency>
        <groupId>com.jcraft</groupId>
        <artifactId>jsch</artifactId>
        <version>0.1.55</version>
    </dependency>
    • 기본적으로 vfs2 의존성을 넣어주면 됩니다.
    • 하지만, jsch 를 넣어주지 않으면 sftp 로 전송을 할 수 없습니다.(vfs2 패키지에서 JSch를 필요로 합니다.)

     

    2. Sample Code

    public void upload(File file) throws FileSystemException {
        FileSystemManager manager = VFS.getManager();
        FileSystemOptions fileOpt = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(fileOpt, false);
    
        try (
                FileObject local = manager.resolveFile(file.getPath());
                FileObject remote = manager.resolveFile("sftp://" + username + ":" + password + "@" + hostName + remoteFilePath, fileOpt);
            )
        {
    
            remote.copyFrom(local, Selectors.SELECT_SELF);
    
            log.info("file upload success");
    
        } catch (Exception e) {
            log.info("file upload failed");
            e.printStackTrace();
        }
    
    }

    copyForm() 메소드가 실행이되면서 remote에 업로드가 됩니다.

    SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(fileOpt, false); 를 통해 UserDirlsRoot를 false하면(default true) home 디렉터리가 아닌 root 디렉터리에서부터 경로를 탐색하여 저장합니다.

    예를 들어 /opt/data에 저장한다고 할 때 UserDirIsRoot = true라면 /home/username/opt/data에 저장을 하고 false라면 /opt/data에 저장하게 됩니다.

     

    * 주의점

    password에 #이나 @들어가면 오류 발생

     

    저작자표시

    'Programming > JAVA' 카테고리의 다른 글

    [pdfbox] PDF 파일을 Image파일로 변환하는 방법  (0) 2023.05.10
    JAVA SHA-256 암호화 방법  (0) 2023.05.10
    [openhtmltopdf] HTML을 PDF로 변환하기  (0) 2023.01.18
    [Apache Tika] 문서파일에서 텍스트 추출하기  (0) 2022.12.15
    애니메이션 이미지인지 아닌지 체크하는 방법(Check image animated or not in JAVA)  (0) 2022.09.16
      'Programming/JAVA' 카테고리의 다른 글
      • [pdfbox] PDF 파일을 Image파일로 변환하는 방법
      • JAVA SHA-256 암호화 방법
      • [openhtmltopdf] HTML을 PDF로 변환하기
      • [Apache Tika] 문서파일에서 텍스트 추출하기
      빈쿵바라기
      빈쿵바라기
      삽질하는 개발자의 좌충우돌 개발기

      티스토리툴바