当前位置: 首页 > 编程日记 > 正文

Java中使用FTPClient上传下载

转自:http://blog.csdn.net/hbcui1984/article/details/2720204

在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件、下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件。

一、上传文件

原理就不介绍了,大家直接看代码吧

[Java] view plaincopy
  1. /** 
  2.  * Description: 向FTP服务器上传文件 
  3.  * @Version1.0 Jul 27, 2008 4:31:09 PM by 崔红保(cuihongbao@d-heaven.com)创建 
  4.  * @param url FTP服务器hostname 
  5.  * @param port FTP服务器端口 
  6.  * @param username FTP登录账号 
  7.  * @param password FTP登录密码 
  8.  * @param path FTP服务器保存目录 
  9.  * @param filename 上传到FTP服务器上的文件名 
  10.  * @param input 输入流 
  11.  * @return 成功返回true,否则返回false 
  12.  */  
  13. public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {  
  14. boolean success = false;  
  15. FTPClient ftp = new FTPClient();  
  16. try {  
  17. int reply;  
  18. ftp.connect(url, port);//连接FTP服务器  
  19. //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器  
  20. ftp.login(username, password);//登录  
  21. reply = ftp.getReplyCode();
  22. if (!FTPReply.isPositiveCompletion(reply)) {  
  23. ftp.disconnect();
  24. return success;  
  25. }
  26. ftp.changeWorkingDirectory(path);
  27. ftp.storeFile(filename, input);
  28. input.close();
  29. ftp.logout();
  30. success = true;  
  31. catch (IOException e) {  
  32. e.printStackTrace();
  33. finally {  
  34. if (ftp.isConnected()) {  
  35. try {  
  36. ftp.disconnect();
  37. catch (IOException ioe) {  
  38. }
  39. }
  40. }
  41. return success;  
  42. }<pre></pre>

下面我们写两个小例子:

1.将本地文件上传到FTP服务器上,代码如下:

[Java] view plaincopy
  1. @Test  
  2. public void testUpLoadFromDisk(){  
  3. try {  
  4. FileInputStream in=new FileInputStream(new File("D:/test.txt"));  
  5. boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", in);  
  6. System.out.println(flag);
  7. catch (FileNotFoundException e) {  
  8. e.printStackTrace();
  9. }
  10. }<pre></pre>

2.在FTP服务器上生成一个文件,并将一个字符串写入到该文件中

[Java] view plaincopy
  1. @Test  
  2. public void testUpLoadFromString(){  
  3. try {  
  4. InputStream input = new ByteArrayInputStream("test ftp".getBytes("utf-8"));  
  5. boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", input);  
  6. System.out.println(flag);
  7. catch (UnsupportedEncodingException e) {  
  8. e.printStackTrace();
  9. }
  10. }<pre></pre>


二、下载文件

从FTP服务器下载文件的代码也很简单,参考如下:

[Java] view plaincopy
  1. /** 
  2.  * Description: 从FTP服务器下载文件 
  3.  * @Version1.0 Jul 27, 2008 5:32:36 PM by 崔红保(cuihongbao@d-heaven.com)创建 
  4.  * @param url FTP服务器hostname 
  5.  * @param port FTP服务器端口 
  6.  * @param username FTP登录账号 
  7.  * @param password FTP登录密码 
  8.  * @param remotePath FTP服务器上的相对路径 
  9.  * @param fileName 要下载的文件名 
  10.  * @param localPath 下载后保存到本地的路径 
  11.  * @return 
  12.  */  
  13. public static boolean downFile(String url, int port,String username, String password, String remotePath,String fileName,String localPath) {  
  14. boolean success = false;  
  15. FTPClient ftp = new FTPClient();  
  16. try {  
  17. int reply;  
  18. ftp.connect(url, port);
  19. //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器  
  20. ftp.login(username, password);//登录  
  21. reply = ftp.getReplyCode();
  22. if (!FTPReply.isPositiveCompletion(reply)) {  
  23. ftp.disconnect();
  24. return success;  
  25. }
  26. ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录  
  27. FTPFile[] fs = ftp.listFiles();
  28. for(FTPFile ff:fs){  
  29. if(ff.getName().equals(fileName)){  
  30. File localFile = new File(localPath+"/"+ff.getName());  
  31. OutputStream is = new FileOutputStream(localFile);   
  32. ftp.retrieveFile(ff.getName(), is);
  33. is.close();
  34. }
  35. }
  36. ftp.logout();
  37. success = true;  
  38. catch (IOException e) {  
  39. e.printStackTrace();
  40. finally {  
  41. if (ftp.isConnected()) {  
  42. try {  
  43. ftp.disconnect();
  44. catch (IOException ioe) {  
  45. }
  46. }
  47. }
  48. return success;  
  49. }<pre></pre>

相关文章:

【建模必备】遗传算法的基本原理与步骤(编码/解码)

如果喜欢这里的内容&#xff0c;你能够给我最大的帮助就是转发&#xff0c;告诉你的朋友&#xff0c;鼓励他们一起来学习。 If you like the content here, you can give me the greatest help is forwarding, tell your friends, encourage them to learn together.

linux中ftp用户,linux中怎么添加ftp用户

Linux下创建用户是很easy的事情了&#xff0c;只不过不经常去做这些操作&#xff0c;时间久了就容易忘记。那么linux中怎么添加ftp用户&#xff0c;下面跟着学习啦小编一起来了解一下吧。linux中怎么添加ftp用户在linux中添加ftp用&#xff0c;并设置相应的权限&#xff0c;操作…

html内通过parentNode来得到上级对象,与此对应的,还有childNodes[x]得到下级对象...

但是对于表格要注意&#xff0c;在<table>和<tr>之间还有一个<tbody>&#xff0c;即使你在构建<table>时没有使用这个<tbody> <table> <tr> <td> <input typebutton valueclick οnclickdeleteItem(this);/> </td>…

lucene查询

1.创建项目(lucene)2.创建SearchIndex类,包名(com.zhishang.lucene)package com.zhishang.lucene;import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.a…

【建模必备】遗传算法的基本原理与步骤(适应度函数与适应度分配)

如果喜欢这里的内容&#xff0c;你能够给我最大的帮助就是转发&#xff0c;告诉你的朋友&#xff0c;鼓励他们一起来学习。 If you like the content here, you can give me the greatest help is forwarding, tell your friends, encourage them to learn together.

linux系统中find怎么用,linux系统中‘find’的详细用法

“find”指令是linux系统下较为常用的指令&#xff0c;它常见的用法我们也需要掌握&#xff0c;下面主要是对‘find’指令的常见用法作一下总结&#xff0c;希望能够对其他人有所帮助。在linux系统下用"ls"指令查看目录如下&#xff1a;1.find指令的一般格式&#xf…

Windows环境下Unicode编程总结和将ANSI转换到Unicode 将Unicode转换到ANSI

Windows环境下Unicode编程总结 UNICODE环境设置在安装Visual Studio时&#xff0c;在选择VC时需要加入unicode选项&#xff0c;保证相关的库文件可以拷贝到system32下。 UNICODE编译设置&#xff1a;C/C, Preprocessor difinitions 去除_MBCS&#xff0c;加_UNICODE,UNICODE在P…

【建模必备】遗传算法的基本原理与步骤(选择)

如果喜欢这里的内容&#xff0c;你能够给我最大的帮助就是转发&#xff0c;告诉你的朋友&#xff0c;鼓励他们一起来学习。 If you like the content here, you can give me the greatest help is forwarding, tell your friends, encourage them to learn together.

linux下的文件和文件夹的权限问题

1 文件和文件夹的权限 文件和文件夹的权限设置的根本目的是控制人对它们的访问。 2 用户分类 本文件的拥有者、本文件所属的grou、其它用户。 3 也就是说 在读写文件或者文件夹时&#xff0c;要看看自己是属于哪一类用户&#xff0c;然后自己是否拥有相应的权限。当没有相应的权…

linux传文件到xp,Linux与Windows XP之间使用FTP互传文件

我是用虚拟机装了Linux&#xff0c;真实系统是Windows XP&#xff0c;在Windows XP下用Serv-u软件架设了FTP服务器&#xff0c;然后我们就可以在虚拟机的Linux下登录该FTP服务器下载或上传文件了&#xff0c;不同的仅是在Linux下是在命令行里输入相关FTP命令来完成的&#xff0…

.net下的富文本编辑器FCKeditor的配置方法(图)原创

.net下的富文本编辑器FCKeditor的配置方法&#xff08;图&#xff09;原创 FCKeditor是一款开源的富文本编辑器&#xff0c;几乎支持所有流行的Web开发语言&#xff0c;版本稳定&#xff0c;用户多&#xff0c;可配置性好。 以前做Java和php的时候就一直用FCKeditor&#xff0c…

SD-WAN行业发展需要VNF演进

与任何成功技术一样&#xff0c;软件定义广域网&#xff08;SD-WAN&#xff09;市场正在经历着与市场意识相关的日益增长的困境&#xff0c;很多厂商将这一术语扩展为自己的传统解决方案或者只是将之作为发展的方向&#xff0c;而目前SD-WAN确实是业界发展的趋势。随着广域网逐…

【建模必备】遗传算法的基本原理与步骤(交叉)

如果喜欢这里的内容&#xff0c;你能够给我最大的帮助就是转发&#xff0c;告诉你的朋友&#xff0c;鼓励他们一起来学习。 If you like the content here, you can give me the greatest help is forwarding, tell your friends, encourage them to learn together.

linux获取近一小时日志,Linux分析日志获取最多访问的前10个IP

apache日志分析可以获得很多有用的信息&#xff0c;现在来试试最基本的&#xff0c;获取最多访问的前10个IP地址及访问次数。既然是统计&#xff0c;那么awk是必不可少的&#xff0c;好用而高效。命令如下&#xff1a;awk ‘{a[$1] 1;} END {for (i in a) printf("%d %s\…

windows xp远程桌面没有反应

今天发现windows xp远程桌面连接没有反应&#xff0c;看了3389端口都打开了。 日志里面有多条RDPDD.DLL Failed to Load&#xff0c;查了一下结果是ati显卡或nv显卡驱动程序造成的&#xff0c;确实是没想到&#xff0c;呵呵。查到解决方法如下&#xff1a;1.取消硬件加速功能&a…

paramiko 模块封装

paramiko 模块封装 #!/usr/bin/env python#codingutf-8 import paramiko, getpass,sys,traceback class ssh_utils(): def login_by_passwd(self, ip, port, username, passwd): self.ip ip self.port port self.username username self.passwd passwd self.pkey None def…

【建模必备】遗传算法的基本原理与步骤(变异)

如果喜欢这里的内容&#xff0c;你能够给我最大的帮助就是转发&#xff0c;告诉你的朋友&#xff0c;鼓励他们一起来学习。 If you like the content here, you can give me the greatest help is forwarding, tell your friends, encourage them to learn together.

执行远程linux命令,linux shell 远程执行命令

经常要部署多台服务器上面的应用&#xff0c;如果一个个机器的登录太麻烦。所有就想到编写一个脚本来部署不同的服务器前提条件&#xff1a;配置ssh免登陆如果不会的请参加我的另外一篇文章 http://blog.csdn.net/chenpy/article/details/30281515两个错误&#xff1a;Pseudo-t…

新的工作电脑 神州优雅A550-i7

新的工作电脑&#xff0c;神州优雅A550-i7&#xff0c;也推荐给大家&#xff0c;只想讲给大家买神州很好&#xff0c;一点也不丢脸&#xff0c;不要为了牌子多花钱&#xff0c;世界上80%的笔记本出自中国&#xff08;包括台湾&#xff09;&#xff0c;我们应该支持价廉物美。 我…

LinkedIn领英发布《2016中国人才趋势报告》

2016年7月12日&#xff0c;职场社交平台LinkedIn&#xff08;领英&#xff09;在北京举行了“引领创时代”2016领英ConnectIn峰会&#xff0c;并在会上发布了《2016中国人才趋势报告》。大会邀请到来自埃森哲、滴滴出行、中外运-敦豪国际航空快件有限公司、中国国际航空股份有限…

【建模必备】遗传算法应用举例(简单的一元函数优化实例)

如果喜欢这里的内容&#xff0c;你能够给我最大的帮助就是转发&#xff0c;告诉你的朋友&#xff0c;鼓励他们一起来学习。 If you like the content here, you can give me the greatest help is forwarding, tell your friends, encourage them to learn together.

linux哪些文件被更新,Linux:如何確定文件是否已被其他進程更新?

6John mentioned the two main routes you can take for watching files under Unix/Linux systems: notification and polling.John提到了在Unix / Linux系統下觀看文件可以采取的兩條主要途徑:通知和輪詢。Notification is when the system itself (usually the kernel) trig…

也谈贝叶斯分类二

源码我已经上传至http://finallyliuyu.download.csdn.net/ 里面包括按洞庭散人的算法实现的Bayes,以及我改进的bayes.还有birdshiver写的二元分词器&#xff0c;这个我也改进了几个bug. 代码比较乱。所以大家要参考我上一篇文章&#xff0c;来做实验 对不住大家了。都是实验性质…

大数据是一座孤单的小岛

大量的数据仍然掌握在个别高薪企事业单位机构中&#xff0c;如何既共享&#xff0c;又保证信息安全&#xff0c;更是挑战。“当前大数据产业存在的问题&#xff0c;一个是商业规则问题&#xff0c;一个是数据交换问题。”数据的属性只有和它的应用结合在一起才有价值&#xff0…

【建模必备】遗传算法应用举例(多元单峰值函数的优化实例)

如果喜欢这里的内容&#xff0c;你能够给我最大的帮助就是转发&#xff0c;告诉你的朋友&#xff0c;鼓励他们一起来学习。 If you like the content here, you can give me the greatest help is forwarding, tell your friends, encourage them to learn together.

Python在linux服务器上解压,python3传文件到linux服务器然后解压

运维开发网 https://www.qedev.com2020-07-09 10:40出处&#xff1a;网络作者&#xff1a;运维开发网整理#!/usr/bin/env python# -*- coding:utf-8 -*-import osimport paramikoimport timefrom scp import SCPClient相关专题&#xff1a;#!/usr/bin/env python# -*- coding:u…

static构造函数的运行

static构造函数是程序被加载时运行的构造函数。 它的运行不能控制&#xff0c;所以一般不常用。 转载于:https://www.cnblogs.com/jany/archive/2009/12/28/1634529.html

【怎样写代码】向现有类型“添加”方法 -- 扩展方法(一):扩展方法概述

如果喜欢这里的内容&#xff0c;你能够给我最大的帮助就是转发&#xff0c;告诉你的朋友&#xff0c;鼓励他们一起来学习。 If you like the content here, you can give me the greatest help is forwarding, tell your friends, encourage them to learn together.

VMware中创建Ubuntu16.0.4虚拟桥连方式无法上网

一、问题描述 在本地VMvare中已经安装了两台虚拟机&#xff0c;网络方式都是桥连&#xff0c;上网都可以自动获取IP地址 和HOST主机是一个号段的 &#xff0c;同为192.168.1.X KingServer1(原始安装) 桥连方式 KingServer2&#xff08;来自KingServer1的克隆&#xff09; 今天…

linux设置终端颜色256,如何设置我的Linux X终端以便Emacs可以访问256种颜色?

根据this,除了将TERM设置为xterm-256color之外,还需要ncurses-term库.好的,this还有其他一些尝试&#xff1a;The xterm in Ubuntu Edgy does not advertise 256 color support bydefault. To fix this you need to install a 256 color terminfo entry,and tell xterm to use …