java로 만든 sftp 접속 및 다운로드
테스트 정보
- java version : openjdk version "1.8.0_292"
- 접속 대상 : 라즈베리파이4
- 접속 pc : mac pro (M1)
- 사용 라이브러리
- com.jcraft » jzlib 1.1.3
- net.i2p.crypto » eddsa 0.2.0
- org.bouncycastle » bcprov-jdk15on 1.60
- org.bouncycastle » bcpkix-jdk15on 1.60
- e.org.slf4j » slf4j-api 1.7.31
라이브러리는 maven을 사용하시면 아래 내용만 추가하시면 됩니다.
<dependency>
<groupId>com.hierynomus</groupId>
<artifactId>sshj</artifactId>
<version>0.27.0</version>
</dependency>
java sample 입니다
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.RemoteResourceInfo;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
/**
* sftp 접속 확인
* net.schmizz.sshj.common.LoggerFactory$1.getLogger ( sshj sftp lib도 추가 필요한 듯 )
* 위에 에러를 해결하기 위해서 추가 jar 파일
* 1.com.jcraft » jzlib 1.1.3
* 2.net.i2p.crypto » eddsa 0.2.0
* 3.org.bouncycastle » bcprov-jdk15on 1.60
* 4.org.bouncycastle » bcpkix-jdk15on 1.60
* 5.org.slf4j » slf4j-api 1.7.31 (maven에서는 1.7.7 이지만 사용가능)
* =============================================
* maven으로 추가하면 위(jar)는 필요없음
* <p>
* <!-- https://mvnrepository.com/artifact/com.hierynomus/sshj -->
* <dependency>
* <groupId>com.hierynomus</groupId>
* <artifactId>sshj</artifactId>
* <version>0.27.0</version>
* </dependency>
*/
public class SSHFTP {
private String remoteHost = "192.168.0.150"; // 접속할 주소
private String username = "pi"; // 접속할 id
private String password = "raspberry"; // 접속할 비밀번호
final int KB = 1024;
final int MB = 1024 * 1024;
// 생성자
public SSHFTP() throws IOException {
String remoteFile = "/home/dwsamba/hard";
String localDir = "src/Download";
String outData = "";
// 파일다운로드 위치 확인 및 생성
File localDir01 = new File(localDir);
if (!localDir01.exists()) {
localDir01.mkdirs();
}
SSHClient sshClient = setupSshj();
SFTPClient sftpClient = sshClient.newSFTPClient();
// sftp 접속된 remoteFile위치에 파일 목록 확인
List<RemoteResourceInfo> list = sftpClient.ls(remoteFile);
System.out.println("remoteFile : " + remoteFile);
for (RemoteResourceInfo info : list) {
System.out.println("FileFullPathName : " + info.getPath());
outData = String.format("FileName : [%s] FileSize : %d (MB)", info.getName(), (info.getAttributes().getSize() / MB));
System.out.println(outData);
// .txt가 포함된 파일이름 필터
if (info.getName().indexOf(".txt") > -1) {
System.out.println("FileFullPathName : " + info.getPath());
// sftp 다운로드 주석 해제 테스트
// sftpClient.get(info.getName(), localDir + fileName);
}
}
sftpClient.close();
sshClient.disconnect();
}
/**
* sftp 접속
* @return
* @throws IOException
*/
private SSHClient setupSshj() throws IOException {
SSHClient client = new SSHClient();
client.addHostKeyVerifier(new PromiscuousVerifier());
// default utf-8
// client.setRemoteCharset(Charset.forName("UTF-8"));
client.connect(remoteHost);
client.authPassword(username, password);
return client;
}
public static void main(String[] args) {
try {
new SSHFTP();
} catch (IOException e) {
e.printStackTrace();
}
}
}
해당 내용은 아래 사이트에서 참고하였습니다.
https://www.baeldung.com/java-file-sftp
'언어 > JAVA' 카테고리의 다른 글
[spring - swagger] springdoc-openapi를 이용한 스프링 부트 3 스웨거 간단 적용 (0) | 2024.04.18 |
---|---|
[spring web] jar 실행시 프로파일 선택 (0) | 2022.04.21 |
[java] classpath 콘솔 실행 라이브러리 추가 (0) | 2021.07.25 |
[java] sftp (SSHJ) 접속 파일 업로드 sample (0) | 2021.07.18 |
[windows 10] 자바 설치 및 환경설정 java8 (0) | 2019.12.03 |