[Java] 使用 Apache的 Commons-net库 实现FTP操作
2016-03-18 21:48
513 查看
因为最近工作中需要用到FTP操作,而手上又没有现成的FTP代码。就去网上找了一下,发现大家都使用Apache的 Commons-net库中的FTPClient。
但是,感觉用起来不太方便。又在网上找到了很多封装过的。觉得也不是很好用。于是就自己写了一个。网上大多是例子都是直接对文件进行操作,而我更需要的是读到内存,或者从内存上写。并且有很多实用单例模式,但是我觉得如果调用比较多的话,可能会出现问题。
FTPUtil
但是,感觉用起来不太方便。又在网上找到了很多封装过的。觉得也不是很好用。于是就自己写了一个。网上大多是例子都是直接对文件进行操作,而我更需要的是读到内存,或者从内存上写。并且有很多实用单例模式,但是我觉得如果调用比较多的话,可能会出现问题。
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
相关文章推荐
- Apache kafka工作原理
- Apache Kafka编程入门指南:Producer
- Apache Kafka编程入门指南:设置分区数和复制因子
- apache 把404页面的url转发给php脚本处理
- Apache Lucene和Solr 5.0发布
- java 通过Apache poi导出excel代码demo实例
- APACHE 多站点配置方法
- CentOS Linux系统下更改Apache默认网站目录
- CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境
- linux将apache转化为系统服务
- Apache配置SSL
- 实现在CMD中用命令运行apache
- SAX解析错误,org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
- opencv和apache和c#的关联
- How-to: Install Apache Zeppelin on CDH
- Linux中的Apache服务查看和启动的方法
- centos7.0 yum 安装php服务器(apache2.4 php 5.4/5.5/5.6/7.0)
- 编译安装apache
- apache和opencv和linq的关联
- commonio和jetty和apache的关联