TelnetOUtputStream os = ftpClient.put(filename);

File file_in = new File(localPath);

FileInputStream is = new FileInputStream(file_in);

byte[] bytes = new byte[1024];

  1. /**
  2. * FTP文件上传与下载
  3. * notice:
  4. * 之所以每次都要连接一次ftp是让它的目录重新返回到相对的根目录,
  5. * 如果复用上次的FtpClient则可能它当前在FTP的目录不是我们想要的
  6. * 目录,所以在FTP上传下载文件时,最好每次都重新登录一下FTP
  7. * @author lgh
  8. */
  9. public class FTPClient {
  10. private FtpClient ftpClient;
  11. private String ip;
  12. private int port;
  13. private String username;
  14. private String password;
  15. public FTPClient() {
  16. }
  17. public FTPClient(String ip, int port, String username, String password) {
  18. this.ip = ip;
  19. this.port = port;
  20. this.username = username;
  21. this.password = password;
  22. }
  23. /**
  24. * 需要备份的文件
  25. * @param list
  26. * @return
  27. */
  28. private List needBackFile(List list, String relativeName) {
  29. List fileNames = new ArrayList();
  30. for (int i = 0; i < list.size(); i++) {
  31. String temp = (String) list.get(i);
  32. if (temp.indexOf(relativeName) > 0) {
  33. fileNames.add(temp);
  34. }
  35. }
  36. return fileNames;
  37. }
  38. public static void main(String[] args) {
  39. FTPClient client = new FTPClient(".....", 21, "...", "....");
  40. try {
  41. //            client.downloadFile("CRM/ccbcrm/", "D://", "CRMClientLog.log", "CRMClientLog.log");
  42. //            client.uploadFile("", "D://", "CRMClientLog.log");
  43. List list = client.getList("csrtestftp/网络/", false);
  44. } catch (Exception ex) {
  45. Logger.getLogger(FTPClient.class.getName()).log(Level.SEVERE, null, ex);
  46. }
  47. }
  48. /**
  49. * 关闭FTP连接
  50. * @throws java.lang.Exception
  51. */
  52. public void closeServer() throws Exception {
  53. ftpClient.closeServer();
  54. }
  55. /**
  56. * 连接ftp服务器
  57. * @param ip
  58. * @param port
  59. * @param user
  60. * @param pwd
  61. * @return
  62. * @throws Exception
  63. */
  64. public boolean connectServer(String ip, int port, String user, String pwd)
  65. throws Exception {
  66. boolean isSuccess = false;
  67. try {
  68. ftpClient = new FtpClient();
  69. ftpClient.openServer(ip, port);
  70. ftpClient.login(user, pwd);
  71. isSuccess = true;
  72. } catch (Exception ex) {
  73. ex.printStackTrace();
  74. }
  75. return isSuccess;
  76. }
  77. /**
  78. * 获得远程下的目录
  79. * @param remotePath 远程目录
  80. * @param fullPath 是否需要完整路径
  81. * @return
  82. */
  83. public List getList(String remotePath, boolean fullPath) {
  84. List list = new ArrayList();
  85. try {
  86. if (connectServer(ip, port, username, password)) {
  87. BufferedReader br = new BufferedReader(new InputStreamReader(ftpClient.nameList(remotePath)));
  88. String temp = ftpClient.getResponseString();
  89. System.out.println(temp);
  90. String readLine = null;
  91. int lastIndex;
  92. if ((lastIndex = remotePath.lastIndexOf("/")) > 0||(lastIndex = remotePath.lastIndexOf("//")) > 0
  93. ||(lastIndex = remotePath.lastIndexOf("\\")) > 0||(lastIndex = remotePath.lastIndexOf(File.separator)) > 0) {
  94. remotePath = remotePath.substring(0, lastIndex);
  95. }   //去掉remotePath的后缀,可能是'/',也有可能是其他符号
  96. while ((readLine = br.readLine()) != null) {
  97. if (!fullPath) {
  98. list.add(readLine.substring(remotePath.length() + 1, readLine.length()));
  99. System.out.println(readLine.substring(remotePath.length() + 1, readLine.length()));
  100. } else {
  101. list.add(readLine);
  102. System.out.println(readLine);
  103. }
  104. }
  105. ftpClient.closeServer();
  106. }
  107. } catch (Exception ex) {
  108. ex.printStackTrace();
  109. }
  110. return list;
  111. }
  112. /**
  113. * 下载文件
  114. * @param remotePath
  115. * @param localPath
  116. * @param filename
  117. * @throws Exception
  118. */
  119. public void downloadFile(String remotePath, String localPath, String remoteFileName, String localFileName) throws Exception {
  120. try {
  121. if (connectServer(ip, port, username, password)) {
  122. if (remotePath.length() != 0) {
  123. ftpClient.cd(remotePath);
  124. }
  125. ftpClient.binary();
  126. TelnetInputStream is = ftpClient.get(remoteFileName);
  127. File fp = new File(localPath);
  128. if (!fp.exists()) {
  129. fp.mkdirs();
  130. }
  131. File file_out = new File(localPath + File.separator + localFileName);
  132. FileOutputStream os = new FileOutputStream(file_out);
  133. byte[] bytes = new byte[1024];
  134. int readBye;
  135. while ((readBye = is.read(bytes)) != -1) {
  136. os.write(bytes, 0, readBye);
  137. }
  138. is.close();
  139. os.close();
  140. ftpClient.closeServer();
  141. }
  142. } catch (Exception ex) {
  143. ex.printStackTrace();
  144. }
  145. }
  146. /**
  147. * 上传文件
  148. * @param remotePath
  149. * @param localPath
  150. * @param filename
  151. * @throws Exception
  152. */
  153. public void uploadFile(String remotePath, String localPath, String filename) throws Exception {
  154. try {
  155. if (connectServer(ip, port, username, password)) {
  156. if (remotePath.length() != 0) {
  157. ftpClient.cd(remotePath);
  158. }
  159. ftpClient.binary();
  160. TelnetOutputStream os = ftpClient.put(filename);
  161. File file_in = new File(localPath + File.separator + filename);
  162. FileInputStream is = new FileInputStream(file_in);
  163. byte[] bytes = new byte[1024];
  164. int readBye;
  165. while ((readBye = is.read(bytes)) != -1) {
  166. os.write(bytes, 0, readBye);
  167. }
  168. is.close();
  169. os.close();
  170. ftpClient.closeServer();
  171. }
  172. } catch (Exception ex) {
  173. ex.printStackTrace();
  174. }
  175. }
  176. /**
  177. * @return the ip
  178. */
  179. public String getIp() {
  180. return ip;
  181. }
  182. /**
  183. * @param ip the ip to set
  184. */
  185. public void setIp(String ip) {
  186. this.ip = ip;
  187. }
  188. /**
  189. * @return the port
  190. */
  191. public int getPort() {
  192. return port;
  193. }
  194. /**
  195. * @param port the port to set
  196. */
  197. public void setPort(int port) {
  198. this.port = port;
  199. }
  200. /**
  201. * @return the username
  202. */
  203. public String getUsername() {
  204. return username;
  205. }
  206. /**
  207. * @param username the username to set
  208. */
  209. public void setUsername(String username) {
  210. this.username = username;
  211. }
  212. /**
  213. * @return the password
  214. */
  215. public String getPassword() {
  216. return password;
  217. }
  218. /**
  219. * @param password the password to set
  220. */
  221. public void setPassword(String password) {
  222. this.password = password;
  223. }
  224. }

ftp uploadFileAction(重要)的更多相关文章

  1. 8.仿阿里云虚拟云服务器的FTP(包括FTP文件夹大小限制)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#iis 原文:http://dnt.dkill.net/Ar ...

  2. Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件

    异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...

  3. 阿里云学生优惠Windows Server 2012 R2安装IIS,ftp等组件,绑定服务器域名,域名解析到服务器,域名备案,以及安装期间错误的解决方案

     前言: 这几天终于还是按耐不住买了一个月阿里云的学生优惠.只要是学生,在学信网上注册过,并且支付宝实名认证,就可以用9块9的价格买阿里云的云服务ECS.确实是相当的优惠. 我买的是Windows S ...

  4. win7下利用ftp实现华为路由器的上传和下载

    win7下利用ftp实现华为路由器的上传和下载 1.  Win7下ftp的安装和配置 (1)开始->控制面板->程序->程序和功能->打开或关闭Windows功能 (2)在Wi ...

  5. Java实现FTP文件与文件夹的上传和下载

    Java实现FTP文件与文件夹的上传和下载 FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".用于Internet上的控制 ...

  6. centos下开启ftp服务

    如果要ftp访问linux需要安装ftp服务,vsftpd是Linux下比较好的的FTP服务器. 一.检查安装vsftp //检查是否安装vsftpd rpm -qa | grep vsftpd // ...

  7. 解决开启服务器防火墙导致ftp不能连接的问题

    在防火墙设置的"高级"选项卡中的"网络连接设置"--"本地连接"--"设置"中添加了"FTP服务器" ...

  8. centos6.5 nginx-1.8.0和ftp搭建图片服务器

    一.Nginx的安装步骤 1.Nginx安装环境: gcc: 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c+ ...

  9. Jenkins配置MSBuild实现自动部署(MSBuild+SVN/Subversion+FTP+BAT)

    所要用到的主要插件: [MSBuild Plugin] 具体操作: 1.配置MSBuild的版本 [系统管理]->[Global Tool Configuration]->[MSBuild ...

随机推荐

  1. 设置html属性为disabled时flask后台获取数据失败

    标签input的值如果不需要用户修改,则设置属性为 readonly,不要设置为 disabled.因为设置disabled会导致flask后端获取不到这个input得value rule_maker ...

  2. knowledgeroot

    knowledgeroot 示例站点 www.globaladmin.cn Knowledgeroot可用于文档管理,知识库管理. 1.基于php开发,支持linux ,windows.2.支持mys ...

  3. DirectShow控制台输出和保存视频设备名称

    #include "windows.h" #include "TCHAR.h" #include <dshow.h> #include <ve ...

  4. 在Ubuntu 12.04上配置iSCSI Target服务

      今天自己按照网上搜来的教程自己在Ubuntu 12.04上配置了iSCSI Target服务,在这里简单地做个纪录.操作系统是全新安装的Ubuntu 12.04,配置一块500 GB的SATA笔记 ...

  5. WPF中Auto与*的差别

    Auto 表示自己主动适应显示内容的宽度, 如自己主动适应文本的宽度,文本有多长,控件就显示多长. * 则表示按比例来分配宽度. <ColumnDefinition Width="3* ...

  6. olede读excel

    注意点:需要比较excel文件中是否有重复列时,需要设置HDR=No,IMEX=1,即把第一列当做数据读取,不然读到的datatable列名会被自动加数字后缀. /// < summary> ...

  7. Linux 中 eclipse 的tomcat端口号被占用

    1.打开ubuntu启动器(快捷键是 win)--键入 sys--找到 系统监视器(System Monitor) 2.打开系统监视器--找到进程中的java 这个进程--结束该进程

  8. js实现div闪烁-------Day46

    近期在学着用easyui,发现框架用起来果然是方便简洁,能弄出这框架的都是大神级别了吧,牛啊.... 今天碰到这个应用能够说是让我很之无语,整出源代码来一看就明确了.可之前却还是感觉很奇妙,我也经常告 ...

  9. 类matlab find函数

    逻辑矩阵,找出元素1并记录其位置索引. int main(int argc, char** argv) { unsigned ][] = { , , , , , , , , , , , , , , , ...

  10. TBS 手册 --phpv 翻译

    为何使用它? 示例 下载 手册 支持 论坛 推荐 秀出你的站点 http://phpv.net/TBS_Manual.htm#html_automatic 网站: http://www.tinybut ...