设为首页 收藏本站
查看: 770|回复: 0

[经验分享] 方便的FTP客户端封装(apache.commons.net.ftp)

[复制链接]

尚未签到

发表于 2016-6-8 10:14:48 | 显示全部楼层 |阅读模式
每次在javaeye聆听诸位达人的教诲时,心里总想着啥时候我也能奉献一段代码,哪怕是那么简陋,于是今天我就贴一段代码上来,欢迎大家一起讨论,以共同学习为目标。
这段代码是对apache.commons.net.ftp的一个封装。
封装原因:项目中多处使用apache.commons.net.ftp时不方便
封装后的功能:
1.统一配置客户端建立连接,项目中直接调用ftp操作函数
2.可以选择手动控制连接,亦可选择自动控制连接
3.现有的功能:上传、下载、列表、删除
具体使用后面我会贴出一个测试类。
现在开始贴代码:
一、FTP客户端封装主类

/**
*
*  FTP客户端
*
* @author bleet (mailto:liuqiang2004456@163.com)
*/
public class FtpClient {
private FTPClient client = null;
private static FtpClient instance = null;
private FtpClientConfig config = null;
private Logger log = null;
/** 当前工作目录,每次关闭连接要回复到null,因为当前类是单例类 */
private String workDirectory = null;
/** 是否手工控制连接 */
private boolean handSwitch = false;
/** true表示已经登录到ftp服务器 */
private boolean ready = false;
/**
*  初始化参数配置及创建commons.net.ftp的客户端
*
*/
private FtpClient(String configFile){
log = Logger.getLogger(getClass());
client = new FTPClient();
config = FtpClientConfig.getInstance(configFile);
/** 日志输出 */
client.addProtocolCommandListener( new  PrintCommandListener( new  PrintWriter(System.out)));
client.setControlEncoding(config.getRemoteEncoding());
// 设置当前工作目录
workDirectory = config.getRootPath();
}
/**
*  获取ftp客户端的实例
* @return
*/
public static FtpClient getInstance(String configFile) {
if(instance == null) instance = new FtpClient(configFile);
return instance;
}
/**
*  连接ftp
* @return
* @throws SocketException
* @throws IOException
*/
private boolean connect() throws SocketException, IOException {
client.connect(config.getServer(), Integer.valueOf(config.getPort()));
int reply;
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)){
client.disconnect();
log.info("FTP server refused connection.");
return false;
}
return true;
}
/**
* 登入ftp
* @return
* @throws IOException
*/
private boolean login() throws IOException{
if (!client.login(config.getUsername(), config.getPassword())) {
client.logout();
log.info("FTP server login fail.");
return false;
}
return true;
}
/**
* 连接然后登入统一入口
* @return
* @throws SocketException
* @throws IOException
*/
public boolean ready() throws SocketException, IOException {
if(connect() && login()){
setConfig();
ready = true;
return true;
}
return false;
}
/**
*  ftp运行环境参数配置
* @throws IOException
*/
private void setConfig() throws IOException{
FTPClientConfig conf = new FTPClientConfig(config.getFTPStyle());
client.configure(conf);
// 被动传输模式
if(config.getPassiveMode())
client.enterLocalPassiveMode();
// 二进制传输模式
if (config.getBinaryFileType())
client.setFileType(FTP.BINARY_FILE_TYPE);
// 设置当前工作目录
client.changeWorkingDirectory(getWorkDirectory());
}
/**
* 关闭连接
* @throws IOException
*/
public void close() throws IOException {
if(client.isConnected()){
client.logout();
client.disconnect();
// 也可设置为null
workDirectory = config.getRootPath();
}
ready = false;
}
/**
* 获取等前工作目录的文件列表
* @return
* @throws IOException
*/
public String[] listFiles() throws IOException {
if (!setReady()) {
return null;
}
FTPFile[] files = client.listFiles();
int filesLength = files.length;
String[] fileNameArr = new String[filesLength];
for (int i = 0; i < filesLength; i++) {
fileNameArr = files.getName();
}
setClose();
return fileNameArr;
}
/**
* 上传文件,文件名方式
* @param path
* @param name
* @return
* @throws IOException
*/
public boolean upload(String path, String name, String remoteName) throws IOException {
if (!setReady()) {
return false;
}
FileInputStream fis = new FileInputStream(path+name);
if(client.storeFile(getWorkDirectory()+remoteName, fis)){
log.info(" upload success !!! ");
fis.close();
setClose();
return true;
}
fis.close();
setClose();
log.info(" upload fail !!! ");
return false;
}
/**
* 上传文件,流方式
* @param path
* @param name
* @return
* @throws IOException
*/
public boolean upload (InputStream stream, String name, String remoteName) throws IOException {
if (!setReady()) {
return false;
}
if(client.storeFile(getWorkDirectory() + remoteName, stream)){
log.info(" upload success !!! ");
stream.close();
setClose();
return true;
}
stream.close();
setClose();
log.info(" upload fail !!! ");
return false;
}
/**
*  下载文件
* @param path
* @param name
* @return
* @throws UnsupportedEncodingException
* @throws IOException
*/
public boolean download (String path, String name) throws UnsupportedEncodingException, IOException {
if (!setReady()) {
return false;
}
FileOutputStream fos = new FileOutputStream(path + name);
if (client.retrieveFile(new String(name.getBytes(config.getLocalEncoding()),config.getRemoteEncoding()), fos)) {
log.info("download success !!! ");
fos.close();
setClose();
return true;
}
fos.close();
setClose();
log.info(" download fail !!! ");
return false;
}
/**
*  删除文件
* @param path
* @param name
* @return
* @throws IOException
*/
public boolean removeFile(String path, String name) throws IOException {
if (!setReady()) {
return false;
}
client.changeWorkingDirectory(config.getRootPath()+path);
if(client.deleteFile(name)){
log.info("remove file success !!! ");
setClose();
return true;
}
setClose();
log.info(" remove file fail !!! ");
return false;
}
/**
* 改变工作目录
* @param path
* @throws IOException
*/
public void setWorkDirectory(String path) throws IOException {
workDirectory = (config.getRootPath()+path);
// 如果是手动控制可以设置改变工作目录
if(handSwitch){
client.changeWorkingDirectory(workDirectory);
}
}
/**
* 创建目录
* @param pathname
* @return
* @throws IOException
*/
public boolean createDirectory(String pathname) throws IOException {
if (!setReady()) {
return false;
}
boolean okFlag = client.makeDirectory(pathname);
setClose();
return okFlag;
}
/**
*  获取当前工作目录
* @return
*/
public String getWorkDirectory() {
return workDirectory;
}
/**
*  准备FTP连接环境
* @return
* @throws SocketException
* @throws IOException
*/
private boolean setReady() throws SocketException, IOException{
if (!ready) {
if (!ready()) {
log.error("Ftp ready fail.");
if(client.isConnected())
client.disconnect();
return false;
}
}
ready = true;
return true;
}
/**
*  设置是否ftp连接
* @throws IOException
*/
private void setClose() throws IOException{
if(!handSwitch) close();
}
/**
*  打开手动连接
*
*/
public void openHandSwitch() {
handSwitch = true;
}
/**
*  关闭手动连接
*
*/
public void closeHandSwitch() {
handSwitch = false;
}
}


二、读取配置文件类

/**
*
*  FTP客户端的配置
*
* @author bleet (mailto:liuqiang2004456@163.com)
*/
public final class FtpClientConfig {
/** ftp服务器地址 */
private String server;
/** ftp服务器端口 */
private String port;
/** ftp服务器用户名 */
private String username;
/** ftp服务器密码 */
private String password;
/** ftp服务器显示风格 一般为unix 或者nt*/
private String FTPStyle;
/** 本地编码格式 */
private String localEncoding;
/** 远程编码格式 */
private String remoteEncoding;
/** 是否设置 passiveMode模式 */
private boolean passiveMode;
/** 是否设置以二进制传输文件 */
private boolean binaryFileType;
/** ftp服务器工作根目录 */
private String rootPath;
/** 配置文件 */
private String file;
/** 单例 */
private static  FtpClientConfig instance = null;
private Logger log;
/**
*  初始化参数
*
*/
private FtpClientConfig(String file) {
try {
log = Logger.getLogger(getClass());
this.file = file;
initConfig();
} catch (Exception e) {
log.error("FTP::读取配置文件错误.");
}
}
/**
* 获取配置实例
* @param file
* @return
*/
public static FtpClientConfig getInstance(String file) {
if(null == instance)
instance = new FtpClientConfig(file);
return instance;
}
/**
*  读取配置文件
* @param prop
* @return
* @throws Exception
*/
private String getProperty(String prop) throws Exception {
BufferedReader br = null;
try {
InputStream inStream = new FileInputStream(new File(file));
br = new BufferedReader(new InputStreamReader(inStream));
String temp = null;
do{
temp = br.readLine();
if (temp == null)
break;
if (temp.startsWith("#"))
continue;
int index = temp.indexOf("=");
if (index == -1)
continue;
String key = temp.substring(0, index).trim();
String value = temp.substring(index + 1).trim();
if (key.equals(prop)) {
br.close();
return value;
}
}while (temp != null);
br.close();
return null;
} catch (Exception e) {
if (br != null)
br.close();
throw e;
}
}
/**
*  设置属性
* @throws Exception
*/
private void initConfig() throws Exception{
setServer(getProperty("server"));
setPort(getProperty("port"));
setUsername(getProperty("username"));
setPassword(getProperty("password"));
setFTPStyle(getProperty("ftpstyle"));
setLocalEncoding(getProperty("localencoding"));
setRemoteEncoding(getProperty("remoteencoding"));
setPassiveMode(getProperty("passivemode"));
setBinaryFileType(getProperty("binaryfiletype"));
setRootPath(getProperty("rootpath"));
}
/**
*  读取二进制传输方式设置
* @return
*/
public boolean getBinaryFileType() {
return binaryFileType;
}
/**
*  默认以二进制形式传输文件
* @param binaryFileType
*/
public void setBinaryFileType(String binaryFileType) {
if(null == binaryFileType){
this.binaryFileType = true;
}else {
if("".equals(binaryFileType.trim())){
this.binaryFileType = true;
}else if ("true".equals(binaryFileType.trim())) {
this.binaryFileType = true;
}else if ("false".equals(binaryFileType.trim())) {
this.binaryFileType = false;
}
}
}
public String getLocalEncoding() {
return localEncoding;
}
/**
*  默认编码为UTF-8
*/
public void setLocalEncoding(String encoding) {
if(null == encoding){
localEncoding = "UTF-8";
}else {
if("".equals(encoding.trim())) localEncoding = "UTF-8";
}
this.localEncoding = encoding.trim();
}
public String getRemoteEncoding() {
return remoteEncoding;
}
/**
*  默认编码为UTF-8
*/
public void setRemoteEncoding(String encoding) {
if(null == encoding){
this.remoteEncoding = "UTF-8";
}else {
if("".equals(encoding.trim())) remoteEncoding = "UTF-8";
}
this.remoteEncoding = encoding.trim();
}
public String getFTPStyle() {
return FTPStyle;
}
/**
*  默认NT风格
* @param style
*/
public void setFTPStyle(String style) {
if(null == style){
this.FTPStyle = FTPClientConfig.SYST_NT;
}else {
if("".equals(style.trim())){
this.FTPStyle = FTPClientConfig.SYST_NT;
}else if ("unix".equals(style.trim())) {
this.FTPStyle = FTPClientConfig.SYST_UNIX;
}else if ("nt".equals(style.trim())) {
this.FTPStyle = FTPClientConfig.SYST_NT;
}
}
}
public boolean getPassiveMode() {
return passiveMode;
}
/**
* 默认支持passiveMode
* @param passiveMode
*/
public void setPassiveMode(String passiveMode) {
if(passiveMode == null){
this.passiveMode = true;
}else {
if("".equals(passiveMode.trim())){
this.passiveMode = true;
}else if ("true".equals(passiveMode.trim())) {
this.passiveMode = true;
}else if ("false".equals(passiveMode.trim())) {
this.passiveMode = false;
}
}
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPort() {
return port;
}
/**
*  默认端口为21
* @param port
*/
public void setPort(String port) {
if( null == port ){
port = "21";
}else {
if("".equals(port.trim())) port = "21";
}
this.port = port.trim();
}
public String getRootPath() {
return rootPath;
}
/**
* 默认根目录为"/"
* @param rootPath
*/
public void setRootPath(String rootPath) {
if( null == rootPath ){
rootPath = "/";
}else {
if("".equals(rootPath.trim())) rootPath = "/";
}
this.rootPath = rootPath.trim();
}
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}


三、配置文件

server=10.1.1.1
port=21
username=username
password=password
ftpstyle=unix
localencoding=GBK
remoteencoding=GBK
passivemode=true
binaryfiletype=true
rootpath=/fileserver/ftp/


四、测试类  其中onconnet表示手动控制连接用法

public class Test {
private static FtpClient client = FtpClient.getInstance("D:/ftpconf.properties");
/**
* @param args
*/
public static void main(String[] args) {
try {
//getFileList();
//download("C:/", "startadmin.sh");
//uoload();
oneconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void getFileList() throws IOException{
String [] res = client.listFiles();
for (int i = 0; i < res.length; i++) {
System.out.println("---------------"+i+"--------------"+res);
}
}
public static void download(String path, String filename) throws IOException{
client.download(path, filename);
}
/**
*  三个变量顺次为 本地路径 本地文件名 远端文件名
* @throws IOException
*/
public static void uoload() throws IOException{
client.upload("C:/", "a.txt", "b.txt");
}
public static void oneconnect() throws IOException{
client.openHandSwitch();
if(!client.ready()){
client.close();
}else {
getFileList();
download("C:/", "startadmin.sh");
uoload();
}
client.close();
client.closeHandSwitch();
}
}


代码贴完了,大家开始拍砖吧!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-227731-1-1.html 上篇帖子: ftp 文件服务器 下篇帖子: Secure FTP, FTP/SSL, SFTP, FTPS, FTP, SCP... What's the difference?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表