Ftp客户端操作示例。
package ftp.service;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPFileFilter;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.ftpserver.ftplet.FtpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
public class FtpClient {
private Logger logger = LoggerFactory.getLogger(getClass());
private FTPClient ftpClient;
/**
* @param ftpHost FTP主机服务器
* @param ftpPassword FTP 登录密码
* @param ftpUserName FTP登录用户名
* @param ftpPort FTP端口 默认为21
*/
public FtpClient(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort, String ftpEncoding) throws FtpException {
try {
ftpClient = new FTPClient();
ftpClient.setControlEncoding(ftpEncoding); // 中文支持
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
ftpClient.disconnect();
throw new FtpException("未连接到FTP,用户名或密码错误。");
}
} catch (Exception e) {
throw new FtpException(e);
}
}
/**
* 查看FTP服务器文件及目录
* listDirectories()仅列出目录, 不包含文件
*/
public void listFtpFile() throws FtpException {
try {
FTPFile[] ftpFiles = ftpClient.listFiles();
for (FTPFile ftpFile : ftpFiles) {
System.out.println(ftpFile.getName());
}
} catch (Exception e) {
throw new FtpException(e);
}
}
/**
* 查看FTP服务器文件及目录(指定path)
* listDirectories()仅列出目录, 不包含文件
*/
public void listFtpFile(String pathname) throws FtpException {
try {
FTPFile[] ftpFiles = ftpClient.listFiles(pathname);
for (FTPFile ftpFile : ftpFiles) {
System.out.println(ftpFile.getName());
}
} catch (Exception e) {
throw new FtpException(e);
}
}
/**
* 查看FTP服务器文件及目录(指定path), 带过滤规则
* listDirectories()仅列出目录, 不包含文件
*/
public void listFtpFile(String pathname, FTPFileFilter filter) throws FtpException {
try {
FTPFile[] ftpFiles = ftpClient.listFiles(pathname, filter);
for (FTPFile ftpFile : ftpFiles) {
System.out.println(ftpFile.getName());
}
} catch (Exception e) {
throw new FtpException(e);
}
}
/**
* 从FTP服务器下载文件
*
* @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
* @param localPath 下载到本地的位置 格式:H:/download
* @param fileName 文件名称
*/
public void downloadFtpFile(String ftpPath,
String localPath, String fileName) throws FtpException {
try {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//默认ASCII_FILE_TYPE, 传输文件会出问题
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
File localFile = new File(localPath + File.separatorChar + fileName);
OutputStream os = new FileOutputStream(localFile);
ftpClient.retrieveFile(fileName, os);
os.close();
} catch (Exception e) {
throw new FtpException(e);
}
}
/**
* Description: 向FTP服务器上传文件
*
* @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
* @param fileName ftp文件名称
* @param input 文件流
*/
public void uploadFile(String ftpPath, String fileName, InputStream input) throws FtpException {
try {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
ftpClient.storeFile(fileName, input);
input.close();
} catch (Exception e) {
throw new FtpException(e);
}
}
public void close(){
try {
ftpClient.logout();
} catch (IOException e) {
logger.error(e.getMessage());
}finally {
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
logger.error(ioe.getMessage());
}
}
}
}
}
maven依赖:
<dependency>
<groupId>org.apache.ftpserver</groupId>
<artifactId>ftpserver-core</artifactId>
<version>1.1.1</version>
</dependency>
|