- 浏览: 1222789 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (242)
- java (58)
- netty (14)
- javascript (21)
- commons (13)
- 读书笔记 (5)
- java测试 (6)
- database (5)
- struts2 (8)
- hibernate (6)
- english (27)
- spring (10)
- 生活 (4)
- 多线程 (4)
- 正则表达式 (1)
- 杂项 (1)
- maven (4)
- 数据库 (10)
- 学习笔记 (1)
- mongodb (1)
- 百度bcs (4)
- 云推送javasdk (2)
- webservice (3)
- IllegalAnnotationException: Two classes have the same XML type name (0)
- drools (3)
- freemarker (3)
- tomcat (1)
- html5 (2)
- mq (11)
- fastjson (3)
- 小算法 (2)
最新评论
-
longxitian:
https://www.cnblogs.com/jeffen/ ...
万恶的Mybatis的EnumTypeHandler -
asialee:
ddnzero 写道博主请问FileUtils这个类是哪个包的 ...
使用mockftpserver进行ftp测试 -
ddnzero:
博主请问FileUtils这个类是哪个包的?还是自己的呢?能放 ...
使用mockftpserver进行ftp测试 -
yizishou:
为什么会intMap.get("bbb") ...
浅谈System.identityHashCode -
liguanqun811:
感觉LogManager打开了所有的LogSegment(文件 ...
jafka学习之LogManager
项目中由于要使用到ftp服务,虽然之前对edtFTPj有研究,但是鉴于edtFTPj版本更新比较慢等原因,没有使用这个包,我们用的功能比较简单,先简单的介绍下。
过程一般是先建立连接,登陆,然后执行命令,最后关闭连接,相对来说比较简单,直接上代码。
需要说明的是,在写测试单元测试的时候,如果不依赖物理的ftp,就需要自己mock一个ftp了,我在网上找到一个mockFtpServer,具体用法大家可以参考。
package com.xxx.aaa.data.ftp; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.SocketException; import java.util.Calendar; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClientConfig; import org.apache.commons.net.ftp.FTPReply; import com.xxx.aaa.common.aaaConfig; import com.xxx.aaa.common.exception.aaaBaseException; import com.xxx.aaa.common.log.LoggerHelper; import com.xxx.aaa.common.util.DateUtils; import com.xxx.aaa.util.aaaConstants; public class FtpClient { private final FtpClientConfig ftpConfig; private final FTPClient client; private boolean ready = false; public FtpClient(FtpClientConfig config) { ftpConfig = config; client = new FTPClient(); } public void begin() throws aaaBaseException { try { if (connect() && login()) { setConfig(); ready = true; } } catch (SocketException e) { LoggerHelper.err(this.getClass(), "Connect socket error!", e); throw new aaaBaseException("Connect socket error,reaseon:" + e.getMessage()); } catch (IOException e) { LoggerHelper.err(this.getClass(), "Login server error!", e); throw new aaaBaseException("Login server error,reaseon:" + e.getMessage()); } } private boolean connect() throws SocketException, IOException { client.connect(ftpConfig.getServer(), ftpConfig.getPort()); int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { client.disconnect(); LoggerHelper.info(this.getClass(), "Error connet server:{} on port:{}", ftpConfig.getServer(), ftpConfig.getPort()); return false; } return true; } private boolean login() throws IOException { boolean loginSuccess = client.login(ftpConfig.getUsername(), ftpConfig.getPassword()); if (!loginSuccess) { client.logout(); LoggerHelper.info(this.getClass(), "Error login username:{} with password:{}", ftpConfig.getUsername(), ftpConfig.getPassword()); return false; } return true; } /** * ftp运行环境参数配置 * * @throws IOException */ private void setConfig() throws IOException { FTPClientConfig conf = new FTPClientConfig(ftpConfig.getFtpStyle()); client.configure(conf); // 被动传输模式 if (ftpConfig.getPassiveMode()) { client.enterLocalPassiveMode(); } // 二进制传输模式 if (ftpConfig.getBinaryFileType()) { client.setFileType(FTP.BINARY_FILE_TYPE); } // 设置当前工作目录 client.changeWorkingDirectory(ftpConfig.getRootPath()); } /** * 下载文件 * * @param path * @param name * @return * @throws UnsupportedEncodingException * @throws IOException */ public String download(String fileName) throws aaaBaseException { if (!ready) { return ""; } String localPath = aaaConfig.getTempDir(); if (!localPath.endsWith("/")) { localPath = localPath + "/"; } File file = null; FileOutputStream fos = null; String destFileName = generateDestFileName(fileName); try { file = new File(localPath + destFileName); FileUtils.forceMkdir(file.getParentFile()); fos = new FileOutputStream(file); client.retrieveFile(fileName, fos); return destFileName; } catch (IOException e) { LoggerHelper.err(this.getClass(), "IO exception, fileName:{},reason:{}", destFileName, e.getMessage()); throw new aaaBaseException(String.format("IO exception, fileName:%s,reason:%s", fileName, e.getMessage())); } finally { IOUtils.closeQuietly(fos); if (file != null) { file.setReadOnly(); } } } private String generateDestFileName(String srcFileName) { Calendar now = Calendar.getInstance(); String yyMMddHH = DateUtils.getDate2String(DateUtils.YYYYMMDD_HH, now.getTime()); return yyMMddHH + "_" + srcFileName; } public String downloadWithMd5(String fileName, String... md5Name) throws aaaBaseException { String downFileFileName = download(fileName); String fileMd5Name = fileName + aaaConstants.MD5_EXTENSION; if (md5Name != null && md5Name.length > 0 && StringUtils.isNotBlank(md5Name[0])) { fileMd5Name = md5Name[0]; } download(fileMd5Name); return downFileFileName; } public String downloadQuiet(String fileName, String... md5Name) throws aaaBaseException { begin(); String downFileFileName = downloadWithMd5(fileName, md5Name); end(); return downFileFileName; } /** * 关闭连接 * * @throws aaaBaseException */ public void end() throws aaaBaseException { try { if (client.isConnected()) { client.logout(); } } catch (IOException e) { LoggerHelper.err(this.getClass(), "Logout error!", e); throw new aaaBaseException("Logout error,reason:" + e.getMessage()); } finally { if (client.isConnected()) { try { client.disconnect(); } catch (IOException e) { LoggerHelper.err(this.getClass(), "Disconnect socket error!", e); throw new aaaBaseException("Disconnect socket error,reason:" + e.getMessage()); } } } } /** * 上传文件到ftp服务上去 * * @author wangye04 */ public boolean upload(String filename) throws aaaBaseException { if (!ready) { throw new aaaBaseException("ftpClient isn't ready!"); } String localPath = aaaConfig.getTempDir(); if (!localPath.endsWith("/")) { localPath = localPath + "/"; } FileInputStream fis = null; try { fis = new FileInputStream(new File(localPath + filename)); return client.storeFile(filename, fis); } catch (Exception e) { LoggerHelper.err(getClass(), "An exception happened, fileName:{},reason:{}", filename, e.getMessage()); throw new aaaBaseException(e); } finally { IOUtils.closeQuietly(fis); } } }
package com.xxx.aaa.data.ftp; import org.apache.commons.net.ftp.FTPClientConfig; import com.xxx.aaa.common.AAAConfig; /** * ftp的配置类 * */ public class FtpClientConfig { /** ftp服务器地址 */ private String server; /** ftp服务器端口 */ private int port; /** ftp服务器用户名 */ private String username; /** ftp服务器密码 */ private String password; /** ftp服务器显示风格 一般为unix 或者nt */ private String ftpStyle; /** 是否设置 passiveMode模式 */ private boolean passiveMode; /** 是否设置以二进制传输文件 */ private boolean binaryFileType; /** ftp服务器工作根目录 */ private String rootPath; /** 单例,上游ftp服务 */ private static FtpClientConfig sourceInstance = null; /** 单例 */ private static FtpClientConfig targetInstance = null; /** * 从联盟获取配置实例 * * @param file * @return */ public static FtpClientConfig getSourceInstance() { if (null == sourceInstance) { sourceInstance = new FtpClientConfig(); sourceInstance.initSourceConfig(); } return sourceInstance; } /** * 给下游系统提供服务 * * @return */ public static FtpClientConfig getTargetInstance() { if (null == targetInstance) { targetInstance = new FtpClientConfig(); targetInstance.initTargetConfig(); } return targetInstance; } /** * 设置源ftp的属性 * * @throws Exception */ private void initSourceConfig() { setServer(AAAConfig.getConfigValueByKey("ftp.server")); setPort(Integer.parseInt(AAAConfig.getConfigValueByKey("ftp.port"))); setUsername(AAAConfig.getConfigValueByKey("ftp.username")); setPassword(AAAConfig.getConfigValueByKey("ftp.password")); setFtpStyle(AAAConfig.getConfigValueByKey("ftp.ftpstyle")); setPassiveMode(AAAConfig.getConfigValueByKey("ftp.passivemode")); setBinaryFileType(AAAConfig.getConfigValueByKey("ftp.binaryfiletype")); setRootPath(AAAConfig.getConfigValueByKey("ftp.rootpath")); } /** * 设置目标ftp的属性 * * @throws Exception */ private void initTargetConfig() { setServer(AAAConfig.getConfigValueByKey("ftp.target.server")); setPort(Integer.parseInt(AAAConfig.getConfigValueByKey("ftp.target.port"))); setUsername(AAAConfig.getConfigValueByKey("ftp.target.username")); setPassword(AAAConfig.getConfigValueByKey("ftp.target.password")); setFtpStyle(AAAConfig.getConfigValueByKey("ftp.target.ftpstyle")); setPassiveMode(AAAConfig.getConfigValueByKey("ftp.target.passivemode")); setBinaryFileType(AAAConfig.getConfigValueByKey("ftp.target.binaryfiletype")); setRootPath(AAAConfig.getConfigValueByKey("ftp.target.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 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 int getPort() { return port; } /** * 默认端口为21 * * @param port */ public void setPort(Integer port) { if (null == port) { port = 21; } this.port = port; } 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; } }
发表评论
-
commons-io之inputstream学习
2010-08-20 19:41 2545ProxyInputStream类的学习 ... -
commons-io之WildcardFileFilter的实现
2010-08-02 11:35 2427上次这个是最后一个FileFilter,没 ... -
commons-io之filefilter学习
2010-07-27 10:23 50081. IOFileFilter接口 这个接口就是 ... -
common-io之Comparator阅读
2010-07-17 00:17 9521. 首先我们来查看AbstractFileCompa ... -
common-io 之ByteArrayOutputStream阅读
2010-07-16 18:56 3061首先我们来分析它里面的的实例变量:buffers: 可以看成是 ... -
一款文件上传信息即时同步刷新的代码的学习
2010-07-06 18:04 1249对下面链接提供的文件上传的代码的学习。 http://mao ... -
FileUpload之FileItem
2010-07-05 18:17 22626FileItem类主要是封装了一个File Item ... -
commons-io之ThresholdingOutputStream 和 DeferredFileOutputStream
2010-06-26 19:44 25101. ThresholdingOutputStream 这个 ... -
Digester 1.1 单元测试之RuleTestCase
2010-02-03 23:32 1297前面还记得有前辈说过看一个开源项目在不懂的情况下要去 ... -
Digester 1.1 源码阅读
2010-02-03 20:36 1468前一段时间我们阅读了Digester的最初始的版本1 ... -
Digester 1.0 源码阅读
2010-02-01 20:15 1554近来在学习tomcat ... -
Digester学习
2010-01-26 00:45 1857近来在学习tomcat的源码,其中有个解析XML的 ...
相关推荐
在Android端实现ftp登陆、登出、获取文件列表、文件(夹)上传下载、创建文件夹等操作。
Java apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出(3.0.1版本),Android可用
commons-net-3.6.jar 包含FTP文件上传下载,读写操作示例,源码解析
编写android 上传文件到ftp服务器功能所需要的Jar包。 commons-net-3.3-3.4-3.5-3.6 四个版本都有了。
apache commons-net-3.5 实现 FTP 上传文件的神器,拿走不谢! 主要涉及上传文件的jar,希望能帮到你。
Java apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出(3.3版本),Android可用
实现FTP文件上传下载必备jar包commons-net.rar,本人亲测,绝对可用。实现实现FTP文件上传下载必备的jar包
用java实现ftp文件上传。使用commons-net-1.4.1.zip。java网络编程的工具包。
FTP commons-net-1.4.1.jar , FTP上传下载文件必要Jar包。
JAVA连接FTP服务器,并上传/下载文件的,使用commons-net包实现ftp服务器的访问,commons-net包封装了一些常见的网络包:ftp,smtp,pop3等..相关包:commons-net-1.4.1.jar ; commons-io-1.4.jar;jakarta-oro-2.0.8.jar
FileUpload 使得在你可以在应用和Servlet中容易的加入强大和高性能的文件上传能力 HttpClient Commons-HttpClient 提供了可以工作于HTTP协议客户端的一个框架. IO IO 是一个 I/O 工具集 Jelly Jelly是一个基于 ...
android 上的ftp服务器例子,通过ApacheFtpServer实现,依赖以下jar包: commons-...太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
里面包含了,Commons-fileupload和smartupload两种上传文件的代码实例。
Java_FTPClient实现文件上传下载,所用到的jar包有: commons-net-1.4.1.jar jakarta-oro.jar
Java代码连接FTP并且实现文件的上传和下载需要的jar文件。
使用apache commons-net包实现文件ftp上传
ftp上传下载文件使用jar包和工具类,有测试使用方法可以进行测试,没有问题。放心用。
java编程如何从ftp服务器上上传文件或下载文件
提供FTP方式上传,下载文件的源码 当然用jdk里的内库的相关包的类也可以完成此项功能 但用这个快速
FTP文件上传Jar包源码查看