使用Commons-net包实现ftp服务器的访问

项目需要实现从某银行的ftp服务器上定时下载数据文件的模块.于是使用apache的commons-net包实现ftp服务器的链接

中间使用了commons的io和Oro包.Io包封装了一些使用的方法,eg:安全的关闭各种流.根据文件的长度得到文件的方便识别的大小(k,m.g).还有一些其它使用的方法,读者可以下载后自行研究..

commons-net包封装了一些常见的网络包:ftp,smtp,pop3等..本文使用了ftp包的功能. 代码如下:


 

java 代码
 
  1. package org.uranus.tools.net.ftp;  
  2.   
  3. import java.io.InputStream;  
  4. import java.util.Properties;  
  5.   
  6. import org.apache.commons.net.ftp.FTPClient;  
  7. import org.apache.commons.net.ftp.FTPClientConfig;  
  8. import org.apache.commons.net.ftp.FTPReply;  
  9. import org.uranus.util.StringUtils;  
  10.   
  11. /** 
  12. * 基于apache组织的commonNet开源组件实现ftp客户端<br> 
  13. * 本程序运行依赖于一个config成员变量属性,其是一个属性(Properties)对象<br> 
  14. * 系统要求这个config对象,必需有如下属性key:<br> 
  15. * server(ftp服务器ip地址或域名)<br> 
  16. * username(登录用户名)<br> 
  17. * password(登录密码)<br> 
  18. * 如下属性key是可选的:<br> 
  19. * systemKey(ftp服务器的操作系统key,如window)<br> 
  20. * serverLanguageCode(ftp服务器使用的语言,如zh)<br> 
  21. * <br> 
  22. * 本程序是线程安全,在每一个上传时都将创建ftp连接,上传结束后再关闭ftp连接<br> 
  23. * 
  24. * 例子:<br> 
  25. * InputStream localIn = new FileInputStream("c:\\计算机.txt");<br> 
  26. * 
  27. * java.util.Properties config = new Properties();<br> 
  28. * config.setProperty("server", "192.168.5.120");<br> 
  29. * config.setProperty("username", "upload");<br> 
  30. * config.setProperty("password", "upload");<br> 
  31. * 
  32. * FtpClientCommonNetImpl client = new FtpClientCommonNetImpl();<br> 
  33. * client.setConfig(config);<br> 
  34. * client.upload(localIn, "/aaa/计算机.txt", true);<br> 
  35. * 
  36. * 
  37. * @author zhangdb 
  38. * 
  39. */  
  40. public class FtpClientCommonNetImpl implements IFtpClient {  
  41. // ftp配置  
  42. private Properties config;  
  43.   
  44. /** 
  45. * 连接到ftp服务器 
  46. * 
  47. * @param server 
  48. * @param userName 
  49. * @param password 
  50. * @return 
  51. * @throws FtpException 
  52. */  
  53. protected FTPClient connectFtpServer(String server, String userName,  
  54. String password) throws FtpException {  
  55. // 创建ftp客户端对象  
  56. FTPClient ftp = new FTPClient();  
  57. try {  
  58. ftp.configure(this.getFTPClientConfig());  
  59. // 连接ftp服务器  
  60. ftp.connect(server);  
  61. // 登录  
  62. ftp.login(userName, password);  
  63.   
  64. // 返回值  
  65. int reply = ftp.getReplyCode();  
  66. if ((!FTPReply.isPositiveCompletion(reply))) {  
  67. ftp.disconnect();  
  68. throw new FtpException("登录ftp服务器失败,请检查server[" + server  
  69. "]、username[" + userName + "]、password[" + password  
  70. "]是否正确!");  
  71. }  
  72.   
  73. return ftp;  
  74. catch (Exception ex) {  
  75. throw new FtpException(ex);  
  76. }  
  77. }  
  78.   
  79. /** 
  80. * 关闭连接 
  81. * 
  82. * @param ftp 
  83. */  
  84. protected void disconnectFtpServer(FTPClient ftp) {  
  85. try {  
  86. ftp.logout();  
  87. ftp.disconnect();  
  88. catch (Exception ex) {  
  89. throw new FtpException(ex);  
  90. }  
  91. }  
  92.   
  93. /** 
  94. * 上传 
  95. */  
  96. public void upload(InputStream localIn, String remoteFilePath)  
  97. throws FtpException {  
  98. String server = this.config.getProperty("server");  
  99. String userName = this.config.getProperty("username");  
  100. String password = this.config.getProperty("password");  
  101. FTPClient ftp = this.connectFtpServer(server, userName, password);  
  102.   
  103. try {  
  104. boolean result = ftp.storeFile(this  
  105. .enCodingRemoteFilePath(remoteFilePath), localIn);  
  106. if (!result) {  
  107. throw new FtpException("文件上传失败!");  
  108. }  
  109. catch (Exception ex) {  
  110. throw new FtpException(ex);  
  111. finally {  
  112. this.disconnectFtpServer(ftp);  
  113. }  
  114. }  
  115.   
  116. /** 
  117. * 上传结束以后关闭输入流 
  118. * 
  119. * @param localIn 
  120. * @param remoteFilePath 
  121. * @param afterUploadCloseInputStream 
  122. * @throws FtpException 
  123. */  
  124. public void upload(InputStream localIn, String remoteFilePath,  
  125. boolean afterUploadCloseInputStream) throws FtpException {  
  126. try {  
  127. // 上传  
  128. this.upload(localIn, remoteFilePath);  
  129. finally {  
  130. if (afterUploadCloseInputStream) {  
  131. if (localIn != null) {  
  132. try {  
  133. localIn.close();  
  134. catch (Exception ex) {  
  135. throw new FtpException(ex);  
  136. }  
  137. }  
  138. }  
  139. }  
  140. }  
  141.   
  142. /** 
  143. * 得到配置 
  144. * 
  145. * @return 
  146. */  
  147. protected FTPClientConfig getFTPClientConfig() {  
  148. // 创建配置对象  
  149. FTPClientConfig conf = new FTPClientConfig(this.config.getProperty(  
  150. "systemKey", FTPClientConfig.SYST_NT));  
  151. conf.setServerLanguageCode(this.config.getProperty(  
  152. "serverLanguageCode""zh"));  
  153. return conf;  
  154. }  
  155.   
  156. /** 
  157. * 远程文件路径编码(上传到ftp上的文件路径) 
  158. * 
  159. * @param remoteFilePath 
  160. * @return 
  161. */  
  162. protected String enCodingRemoteFilePath(String remoteFilePath) {  
  163. return StringUtils.gbkToIso8859EnCoding(remoteFilePath);  
  164. }  
  165.   
  166. public Properties getConfig() {  
  167. return config;  
  168. }  
  169.   
  170. public void setConfig(Properties config) {  
  171. this.config = config;  
  172. }  
  173.   
  174. }   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值