Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package th.in.oneauthen;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import org.apache.commons.net.ftp.FTP;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPFile;
- public class sftp {
- public static void main(String[] args) {
- String server = "161.246.35.229";
- int port = 22;
- String user = "medserv";
- String pass = "MeddyCall2018";
- FTPClient ftpClient = new FTPClient();
- try {
- // connect and login to the server
- ftpClient.connect(server, port);
- ftpClient.login(user, pass);
- // use local passive mode to pass firewall
- ftpClient.enterLocalPassiveMode();
- System.out.println("Connected");
- String remoteDirPath = "/Test";
- String saveDirPath = "E:/Test/Download/FTP";
- downloadDirectory(ftpClient, remoteDirPath, "", saveDirPath);
- // log out and disconnect from the server
- ftpClient.logout();
- ftpClient.disconnect();
- System.out.println("Disconnected");
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- public static void downloadDirectory(FTPClient ftpClient, String parentDir,
- String currentDir, String saveDir) throws IOException {
- String dirToList = parentDir;
- if (!currentDir.equals("")) {
- dirToList += "/" + currentDir;
- }
- FTPFile[] subFiles = ftpClient.listFiles(dirToList);
- if (subFiles != null && subFiles.length > 0) {
- for (FTPFile aFile : subFiles) {
- String currentFileName = aFile.getName();
- if (currentFileName.equals(".") || currentFileName.equals("..")) {
- // skip parent directory and the directory itself
- continue;
- }
- String filePath = parentDir + "/" + currentDir + "/"
- + currentFileName;
- if (currentDir.equals("")) {
- filePath = parentDir + "/" + currentFileName;
- }
- String newDirPath = saveDir + parentDir + File.separator
- + currentDir + File.separator + currentFileName;
- if (currentDir.equals("")) {
- newDirPath = saveDir + parentDir + File.separator
- + currentFileName;
- }
- if (aFile.isDirectory()) {
- // create the directory in saveDir
- File newDir = new File(newDirPath);
- boolean created = newDir.mkdirs();
- if (created) {
- System.out.println("CREATED the directory: " + newDirPath);
- } else {
- System.out.println("COULD NOT create the directory: " + newDirPath);
- }
- // download the sub directory
- downloadDirectory(ftpClient, dirToList, currentFileName,
- saveDir);
- } else {
- // download the file
- boolean success = downloadSingleFile(ftpClient, filePath,
- newDirPath);
- if (success) {
- System.out.println("DOWNLOADED the file: " + filePath);
- } else {
- System.out.println("COULD NOT download the file: "
- + filePath);
- }
- }
- }
- }
- }
- public static boolean downloadSingleFile(FTPClient ftpClient,
- String remoteFilePath, String savePath) throws IOException {
- File downloadFile = new File(savePath);
- File parentDir = downloadFile.getParentFile();
- if (!parentDir.exists()) {
- parentDir.mkdir();
- }
- OutputStream outputStream = new BufferedOutputStream(
- new FileOutputStream(downloadFile));
- try {
- ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- return ftpClient.retrieveFile(remoteFilePath, outputStream);
- } catch (IOException ex) {
- throw ex;
- } finally {
- if (outputStream != null) {
- outputStream.close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement