因为最近工作中需要用到FTP操作,而手上又没有现成的FTP代码。就去网上找了一下,发现大家都使用Apache的 Commons-net库中的FTPClient。

但是,感觉用起来不太方便。又在网上找到了很多封装过的。觉得也不是很好用。于是就自己写了一个。网上大多是例子都是直接对文件进行操作,而我更需要的是读到内存,或者从内存上写。并且有很多实用单例模式,但是我觉得如果调用比较多的话,可能会出现问题。

 package com.best.oasis.util.helper;

 /**
* 封装了一些FTP操作
* Created by bl05973 on 2016/3/11.
*/ import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List; import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPCmd;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger; public class FTPUtil {
private static Logger logger = Logger.getLogger(FTPUtil.class); private static FTPClient getConnection() {
FTPClient client = new FTPClient();
client.setControlEncoding("UTF-8");
client.setDataTimeout(30000);
client.setDefaultTimeout(30000);
return client;
} public static FTPClient getConnection(String host) throws IOException {
FTPClient client = getConnection();
client.connect(host);
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
throw new IOException("connect error");
}
return client;
}
public static FTPClient getConnection(String host, int port) throws IOException {
FTPClient client = getConnection();
client.connect(host, port);
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
throw new IOException("connect error");
}
return client;
} public static FTPClient getConnection(String host, String username, String password) throws
IOException {
FTPClient client= getConnection(host);
if (StringUtil.isNotBlank(username)) {
client.login(username, password);
}
//if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
// throw new IOException("login error");
//}
return client;
} public static FTPClient getConnection(String host, int port, String username, String password)
throws IOException {
FTPClient client = getConnection(host, port);
if (StringUtil.isNotBlank(username)) {
client.login(username, password);
}
//if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
// throw new IOException("login error");
//}
return client;
} /**
* 移动文件(若目标文件存在则不移动,并返回false)
*/
public static boolean moveFile(String curFileName, String targetFileName, FTPClient client)
throws IOException {
int reply;
reply = client.sendCommand(FTPCmd.RNFR, curFileName);
if (FTPReply.isNegativePermanent(reply)) {
//logger.error("FTP move file error. code:" + reply);
System.out.println("FTP move file error. code:" + reply);
return false;
}
reply = client.sendCommand(FTPCmd.RNTO, targetFileName);
if (FTPReply.isNegativePermanent(reply)) {
//logger.error("FTP move file error. code:" + reply);
System.out.println("FTP move file error. code:" + reply);
return false;
}
return true;
} /**
* 读取文件列表
*/
public static List<String> getFileNameList(FTPClient client) throws IOException {
FTPFile[] files = client.listFiles();
List<String> fileNameList = new ArrayList<>();
for (FTPFile file : files) {
if (file.isFile()) {
fileNameList.add(file.getName());
}
}
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
throw new IOException("get file name list error");
}
return fileNameList;
} /**
* 读文件
*/
public static String readFile(String path, FTPClient client) throws IOException {
client.setFileType(FTP.EBCDIC_FILE_TYPE);
InputStream is = client.retrieveFileStream(path);
if (is == null) {
return null;
}
BufferedReader bf = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String str;
while ((str = bf.readLine()) != null) {
sb.append(str).append("\n");
}
bf.close();
client.completePendingCommand();
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
throw new IOException("Remote file net closed success");
}
return sb.toString();
} @Deprecated
static boolean downFile(String remotePath, String localPath, FTPClient client)
throws IOException {
FileOutputStream fos = new FileOutputStream(localPath);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.retrieveFile(remotePath, fos);
client.completePendingCommand();
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
throw new IOException("Remote file net closed success");
}
return false;
} /**
* 写文件
*/
public static boolean storeAsFile(String context, String remotePath, FTPClient client)
throws IOException {
OutputStream out = client.storeFileStream(remotePath);
OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
writer.write(context);
writer.flush();
writer.close();
return true;
} public static void close(FTPClient client) {
try {
if (client != null) {
client.disconnect();
}
} catch (IOException e) { }
}
}

FTPUtil

最新文章

  1. JavaScript数据类型 typeof, null, 和 undefined
  2. APC注入(Ring3层)
  3. postman使用教程
  4. DataTable 导出Excel 下载 (NPOI)
  5. Labview学习之波形图表的历史数据
  6. IOS UITextView支持输入、复制、粘贴、剪切自定义表情
  7. 2018-2019-2 网络对抗技术 20165325 Exp1 PC平台逆向破解
  8. springboot 学习之路 3( 集成mybatis )
  9. 补充:CSS选择器样式的规范!
  10. 刘志梅201771010115.《面向对象程序设计(java)》第六周学习总结
  11. System.InvalidOperationException: 可为空的对象必须具有一个值。
  12. python3之requests
  13. 【pyqtgraph绘图】在pyqtgraph中绘图
  14. Route学习笔记之Area的Route注册
  15. 解决Pycharm添加虚拟解释器的报错问题
  16. Postman—构建工作流
  17. Sprint7
  18. vs2013 调试libevent 源码
  19. 一个百度MAP导航的基础封装
  20. BZOJ2936 Codevs3634 POI1999 积水 【并查集】*

热门文章

  1. Hadoop HA 与 Federation
  2. docker images镜像无法删除
  3. JS中常用开发知识点
  4. JavaScript div 上下运动实例
  5. Mysql学习总结(32)——MySQL分页技术详解
  6. ASP.NET-入门
  7. POJ 1107
  8. 保留全部Android crash信息
  9. Edison Chou
  10. python-Pymyslql-requests_html:把腾讯新闻的今日推荐和链接存进数据库