Secure FTP (Güvenli Dosya Taşıma Protokolü) , yani SFTP,
SSH kullanarak dosya transferi yapan bir dosya aktarım protokolüdür.
SSH'ın sağladığı güvenlik özellikleri,
FTP'den farklı olarak SFTP'yi güvenli hale getirir.
FTP'nin
RSA ile güçlendirilmiş halidir.
TCP üzerinden çalışır.
Döküman yükleme ve İndirme kodları aşağıdaki gibidir.
......
SftpDownlaod.java
C:\Users\912867\Documents\NetBeansProjects\SftpDocumentTransfer\pom.xml |
xml version="1.0" encoding="UTF-8"?>
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
>4.0.0>
>com.blogtest.ex>
>SftpDocumentTransfer>
>1.0-SNAPSHOT>
>jar>
>
>
>com.jcraft>
>jsch>
>0.1.50>
>jar>
>
>
>
>UTF-8>
>1.7>
>1.7>
>
>
SftpDocumentTransfer\src\main\java\com\blogtest\ex\sftpdocumenttransfer\SftpDownlaod.java |
package com.blogtest.ex.sftpdocumenttransfer;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
@author
public class SftpDownlaod {
public SftpDownlaod() {
}
@param args
public static void main(String[] args) {
String SFTPHOST = "HOSTNAME-OR-IP";
int SFTPPORT = 22;
String SFTPUSER = "USERNAME";
String SFTPPASS = "PASSWORD";
String SFTPWORKINGDIR = "/home/oracle/Desktop";
String fileName = "xxx.pdf";
downloadFile(SFTPHOST, SFTPPORT, SFTPUSER, SFTPPASS, SFTPWORKINGDIR, fileName, "C:/");
}
@param SFTPHOST
@param SFTPPORT
@param SFTPUSER
@param SFTPPASS
@param SFTPWORKINGDIR
@param fileName
@param DownloadPath
public static void downloadFile(String SFTPHOST, int SFTPPORT,
String SFTPUSER,
String SFTPPASS,
String SFTPWORKINGDIR,
String fileName,
String DownloadPath) {
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try {
System.out.println(" START : " + new Date());
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd(SFTPWORKINGDIR);
byte[] buffer = new byte[1024];
BufferedOutputStream bos;
try (BufferedInputStream bis = new BufferedInputStream(channelSftp.get(fileName))) {
File newFile = new File(DownloadPath + fileName);
OutputStream os = new FileOutputStream(newFile);
bos = new BufferedOutputStream(os);
int readCount;
while ((readCount = bis.read(buffer)) > 0) {
System.out.println("Writing: ");
bos.write(buffer, 0, readCount);
} System.out.println(" STOP : " + new Date());
}
bos.close();
} catch (JSchException | SftpException | IOException ex) {
ex.printStackTrace(System.err);
}
channel.disconnect();
session.disconnect();
System.out.println("process has been completed");
}
}
SftpDocumentTransfer\src\main\java\com\blogtest\ex\sftpdocumenttransfer\SftpUpload.java |
package com.blogtest.ex.sftpdocumenttransfer;
import static com.blogtest.ex.sftpdocumenttransfer.SftpDownlaod.downloadFile;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@author
public class SftpUpload {
public static void main(String[] args) {
String SFTPHOST = "HOSTNAME-OR-IP";
String SFTPUSER = "USERNAME";
String SFTPPASS = "PASSWORD";
String SFTPWORKINGDIR = "/home/TEST";
String fileToTransfer = "C:\\user_xsd.html";
upload(SFTPHOST,
SFTPUSER,
SFTPPASS,
SFTPWORKINGDIR,
fileToTransfer);
}
@param SFTPHOST
@param SFTPUSER
@param SFTPPASS
@param SFTPWORKINGDIR
@param FILETOTRANSFER
public static void upload(String SFTPHOST, String SFTPUSER, String SFTPPASS, String SFTPWORKINGDIR, String FILETOTRANSFER) {
try {
int SFTPPORT = 22;
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd(SFTPWORKINGDIR);
File f = new File(FILETOTRANSFER);
channelSftp.put(new FileInputStream(f), f.getName());
} catch (JSchException | SftpException | FileNotFoundException ex) {
ex.printStackTrace(System.err);
}
channel.disconnect();
session.disconnect();
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
Hiç yorum yok:
Yorum Gönder