java ftp linux_一个java 下载FTP( Linux)服务器文件夹的例子 | 学步园

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPClientConfig;

import org.apache.commons.net.ftp.FTPFile;

public class DownFTPFile {

private String hostname;

private String username;

private String password;

private String localBaseDir;

private String remoteBaseDir;

private static FTPClient ftpClient = new FTPClient();

public static void main(String[] args) {

String hostname = "172.18.72.66";

String username = "sfdc3";

String password = "sfdcadmin";

String localBaseDir = "c:/temp/";

String remoteBaseDir = "ptsinstall/v3.20.06";

DownFTPFile downFTPClient = new DownFTPFile();

downFTPClient.setHostname(hostname);

downFTPClient.setUsername(username);

downFTPClient.setPassword(password);

downFTPClient.setLocalBaseDir(localBaseDir);

downFTPClient.setRemoteBaseDir(remoteBaseDir);

downFTPClient.startDown();

}

private void startDown() {

try {

ftpClient.connect(hostname);

ftpClient.login(username, password);

FTPClientConfig conf = new FTPClientConfig( FTPClientConfig.SYST_UNIX);

ftpClient.configure(conf);

} catch (Exception e1) {

e1.printStackTrace();

}

try {

FTPFile[] files = null;

boolean changedir = ftpClient.changeWorkingDirectory(remoteBaseDir);

if ( changedir ){

files = ftpClient.listFiles();

for (int i = 0; i < files.length; i++) {

downloadFile(files[i], localBaseDir, remoteBaseDir);

}

}

} catch (Exception e) {

System.out.println(e);

}

}

private void downloadFile(FTPFile ftpFile, String relativeLocalPath,String relativeRemotePath) {

if (ftpFile.isFile()) {//down file

if (ftpFile.getName().indexOf("?") == -1) {

OutputStream outputStream = null;

try {

outputStream = new FileOutputStream(relativeLocalPath+ ftpFile.getName());

ftpClient.retrieveFile(ftpFile.getName(), outputStream);

outputStream.flush();

outputStream.close();

} catch (Exception e) {

System.err.println( e );

} finally {

try {

if (outputStream != null)

outputStream.close();

} catch (IOException e) {

System.out.println("ShowasaFile");

}

}

}

} else { //deal dirctory

String newlocalRelatePath = relativeLocalPath + ftpFile.getName();

String newRemote = new String(relativeRemotePath+ ftpFile.getName().toString());

File fl = new File(newlocalRelatePath);

if (!fl.exists()) {

fl.mkdirs();

}

try {

newlocalRelatePath = newlocalRelatePath + '/';

newRemote = newRemote + "/";

String currentWorkDir = ftpFile.getName().toString();

//enter relative workdirectory

boolean changedir = ftpClient.changeWorkingDirectory(currentWorkDir);

if (changedir) {

FTPFile[] files = null;

files = ftpClient.listFiles();

for (int i = 0; i < files.length; i++) {

downloadFile(files[i], newlocalRelatePath, newRemote);

}

}

//return parent directory

if (changedir)

ftpClient.changeToParentDirectory();

} catch (Exception e) {

System.out.println(e);

}

}

}

public String getHostname() {

return hostname;

}

public void setHostname(String hostname) {

this.hostname = hostname;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getLocalBaseDir() {

return localBaseDir;

}

public void setLocalBaseDir(String localBaseDir) {

this.localBaseDir = localBaseDir;

}

public String getRemoteBaseDir() {

return remoteBaseDir;

}

public void setRemoteBaseDir(String remoteBaseDir) {

this.remoteBaseDir = remoteBaseDir;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值