공식 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>
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 |