package ftptest2;
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.FTPFile;
import org.apache.log4j.Logger;
public class FtpTool {
private static final Logger log = Logger.getLogger(FtpUtil.class);
private static String SERVER_NAME = "192.168.18.43";
private static final int SERVER_PORT = 21;
private static String LOGIN_NAME = "test";
private static String LOGIN_PASSWD = "test";
private static String LOCAL_PATH = "E:\\data\\";
private static String REMOTE_PATH = ".";// 代表根目录
// private static String REMOTE_PATH = "ttttt";
private static FTPClient ftpClient = new FTPClient();
public static void main(String[] args) throws Exception {
FtpTool downFTPClient = new FtpTool();
downFTPClient.getConnection();
downloadAll(downFTPClient, LOCAL_PATH, REMOTE_PATH, true);
downFTPClient.disConnection();
}
// 删除全部文件
private static void downloadAll(FtpTool downFTPClient, String localPath,
String remotePath, boolean isDeleteFile) {
try {
FTPFile[] files = null;
boolean changedir = ftpClient.changeWorkingDirectory(REMOTE_PATH);
if (changedir) {
files = ftpClient.listFiles();
for (FTPFile file : files) {
downFTPClient.downloadFile(file, LOCAL_PATH, REMOTE_PATH,
true);
}
}
} catch (Exception e) {
System.out.println(e);
}
}
/**
* 断开连接
*
* @throws Exception
*/
public void disConnection() throws Exception {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
}
/**
* 建立连接
*/
private void getConnection() {
try {
// 解决中文乱码问题
ftpClient.setControlEncoding("utf-8");
ftpClient.connect(SERVER_NAME, SERVER_PORT);
ftpClient.login(LOGIN_NAME, LOGIN_PASSWD);
} catch (Exception e1) {
e1.printStackTrace();
}
}
/**
* 下载文件
*/
private void downloadFile(FTPFile ftpFile, String relativeLocalPath,
String relativeRemotePath, boolean isDeleteFile) {
if (ftpFile.isFile()) {// down file
if (ftpFile.getName().indexOf("?") == -1) {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(relativeLocalPath
+ ftpFile.getName());
log.info("download fileName: " + ftpFile.getName());
ftpClient.retrieveFile(ftpFile.getName(), outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
System.err.println(e);
} finally {
try {
// 删除文件
if (isDeleteFile) {
log.info("delete fileName : " + ftpFile.getName());
ftpClient.deleteFile(ftpFile.getName());
}
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,
isDeleteFile);
// removeDirectory()方法只能删除空目录,因此要先删除目录中的文件。
log.info("delete directory : " + files[i].getName());
ftpClient.removeDirectory(files[i].getName());
}
// 删除当前目录
log.info("delete directory : ");
ftpClient.removeDirectory(".");
}
// return parent directory
if (changedir)
ftpClient.changeToParentDirectory();
} catch (Exception e) {
System.out.println(e);
}
}
}
}
FTPClient下载文件及目录
最新推荐文章于 2022-08-06 18:15:02 发布