Java实现FTP上传下载文件的工具包有很多,这里我采用Java自带的API,实现FTP上传下载文件。另外JDK1.7以前的版本与其之后版本的API有了较大的改变了。

例如:

JDK1.7之前 JDK1.7
ftpClient = new FtpClinet() ftpClient = FtpClient.create(ip)
ftpclient.login(user,password) ftpclient.login(user,null,password)
ftpclient.binary() ftpClient.setBinaryType();

一. 连接FTP服务器

   1: public class FTPUtil {

   2:     //FTP服务器IP地址

   3:     public final static String FTP_HOST = "10.103.240.255";

   4:     

   5:     //FTP服务器端口

   6:     public final static int FTP_PORT = 21;

   7:     

   8:     //FTP服务器用户名

   9:     public final static String FTP_USER = "bloodHunter";

  10:     

  11:     //密码

  12:     public final static String FTP_PASSWORD = "wbljy";

  13:     

  14:     

  15:     public static FtpClient getConnect()

  16:     {

  17:         try {

  18:             FtpClient ftpClient = FtpClient.create(FTP_HOST);

  19:             ftpClient.login(FTP_USER, FTP_PASSWORD.toCharArray());

  20:             return ftpClient;

  21:         } catch (FtpProtocolException e) {

  22:             // TODO Auto-generated catch block

  23:             e.printStackTrace();

  24:             System.out.println("Connect to FTP Server fail!");

  25:             return null;

  26:         } catch (IOException e) {

  27:             // TODO Auto-generated catch block

  28:             e.printStackTrace();

  29:             System.out.println("Connect to FTP Server fail!");

  30:             return null;

  31:         }

  32:         

  33:     }

  34: }

二. 上传文件

   1: /*

   2:      * ftp file upload

   3:      * @param path 上传文件的路径

   4:      * @param fileName 上传文件名称

   5:      * @return 上传成功返回true,否则返回false

   6:      * */

   7:     

   8:     public static boolean FtpUpload(String path,String fileName)

   9:     {

  10:         TelnetOutputStream os = null;

  11:         FileInputStream is = null;

  12:         FtpClient ftpClient = getConnect();

  13:         try {

  14:             ftpClient.setBinaryType();

  15:             os = (TelnetOutputStream) ftpClient.putFileStream(fileName, true);

  16:             is = new FileInputStream(new File(path));

  17:             byte[] buffer = new byte[1024];

  18:             int c;

  19:             while((c = is.read(buffer)) != -1)

  20:             {

  21:                 os.write(buffer,0,c);

  22:             }

  23:             System.out.println("Upload Success!");

  24:             return true;

  25:         } catch (Exception e) {

  26:             // TODO: handle exception

  27:             e.printStackTrace();

  28:             System.out.println("Upload fail!");

  29:             return false;

  30:         }finally{

  31:             try {

  32:                 ftpClient.close();

  33:                 is.close();

  34:                 os.close();

  35:             } catch (IOException e) {

  36:                 // TODO Auto-generated catch block

  37:                 e.printStackTrace();

  38:             }

  39:         }

  40:     }

三. 下载文件

   1: /*

   2:      * ftp file download

   3:      * @param path 下载文件的保存路径

   4:      * @param fileName 下载文件名称

   5:      * @return 下载成功返回true,否则返回false

   6:      * */

   7:     public static boolean FtpDownload(String path,String fileName)

   8:     {

   9:         FileInputStream is = null;

  10:         FileOutputStream os = null;

  11:         FtpClient ftpClient = getConnect();

  12:         try {

  13:             is =  (FileInputStream) ftpClient.getFileStream(fileName);

  14:             os = new FileOutputStream(new File(path));

  15:             byte[] buffer = new byte[1024];

  16:             int c;

  17:             while((c = is.read(buffer)) != -1)

  18:             {

  19:                 os.write(buffer,0,c);

  20:             }

  21:             System.out.println("Download Success!");

  22:             return true;

  23:         } catch (FtpProtocolException e) {

  24:             // TODO Auto-generated catch block

  25:             e.printStackTrace();

  26:             System.out.println("Download fail!");

  27:             return false;

  28:         } catch (IOException e) {

  29:             // TODO Auto-generated catch block

  30:             e.printStackTrace();

  31:             System.out.println("Download fail");

  32:             return false;

  33:         }catch (Exception e) {

  34:             // TODO: handle exception

  35:             e.printStackTrace();

  36:             return false;

  37:         }

  38:         finally{

  39:             try {

  40:                 is.close();

  41:                 os.close();

  42:                 ftpClient.close();

  43:             } catch (IOException e) {

  44:                 // TODO Auto-generated catch block

  45:                 e.printStackTrace();

  46:             }

  47:         }

  48:     }

四. 遍历FTP目录文件

   1: /*

   2:      * FTP getFileList

   3:      * @param filenames       保存遍历的文件名

   4:      * @param path    遍历目录的路径

   5:      * */

   6:     public static void getFtpFileList(List<String> filenames,String path){

   7:         //DataInputStream ds = null;

   8:         BufferedReader ds = null;

   9:         FtpClient ftpClient = getConnect();

  10:         try {

  11:             ds = new BufferedReader(new InputStreamReader(ftpClient.nameList(path),"ISO-8859-1"));

  12:             String line = "";

  13:             while((line = ds.readLine())!=null){

  14:                 line = new String(line.getBytes("ISO-8859-1"),"GBK");

  15:                 String name[] = line.split("/");

  16:                 filenames.add(name[name.length - 1]);

  17:             }

  18:         } catch (FtpProtocolException e) {

  19:             // TODO Auto-generated catch block

  20:             e.printStackTrace();

  21:         } catch (IOException e) {

  22:             // TODO Auto-generated catch block

  23:             e.printStackTrace();

  24:         }finally{

  25:             try {

  26:                 ds.close();

  27:                 ftpClient.close();

  28:             } catch (IOException e) {

  29:                 // TODO Auto-generated catch block

  30:                 e.printStackTrace();

  31:             }

  32:         }

  33:     }

Java 利用FTP上传,下载文件,遍历文件目录的更多相关文章

  1. JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)

    package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...

  2. 2.6 利用FTP上传所有文件

    利用FTP上传所有文件 import os,ftptools class UploadAll(ftptools.FtpTools): #继承上一篇写的Ftptools '''upload an ent ...

  3. FTP上传下载文件(函数简易版)

    FTP上传下载文件(函数简易版) # 服务端 import socket import json import hashlib import struct import os user_dic = { ...

  4. java客户端调用ftp上传下载文件

    1:java客户端上传,下载文件. package com.li.utils; import java.io.File; import java.io.FileInputStream; import ...

  5. java web service 上传下载文件

    1.新建动态web工程youmeFileServer,新建包com,里面新建类FileProgress package com; import java.io.FileInputStream; imp ...

  6. 【转】Java IOUtils方式上传下载文件 on HDFS

    [From]https://www.cnblogs.com/areyouready/p/9795442.html package com.css.hdfs04; import java.io.File ...

  7. Java实现FTP上传下载功能

    Java FTP客户端工具包很多,在此我选用的Apache的FTPClient.这个包的获取可以通过http://commons.apache.org/net/来获取,我使用的是最新的commons- ...

  8. shell ftp上传下载文件

    1. ftp自动登录批量下载文件. #####从ftp服务器上的/home/data 到 本地/home/databackup#### #!/bin/bash ftp -n<<! open ...

  9. 在linux命令行利用SecureCRT上传下载文件

    一般来说,linux服务器大多是通过ssh客户端来进行远程的登陆和管理的,使用ssh登陆linux主机以后,如何能够快速的和本地机器进行文件的交互呢,也就是上传和下载文件到服务器和本地?与ssh有关的 ...

随机推荐

  1. codeforces 667A A. Pouring Rain(水题)

    题目链接: A. Pouring Rain time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. ubuntu下tesseract 4.0安装及参数使用

    tesseract是一个开源的OCR引擎,最初是由惠普公司开发用来作为其平板扫描仪的OCR引擎,2005年惠普将其开源出来,之后google接手负责维护.目前稳定的版本是3.0.4.0版本加入了基 ...

  3. 频繁GC会造成卡顿

    频繁GC会造成卡顿 https://www.cnblogs.com/qcloud1001/p/9525078.html 一款app除了要有令人惊叹的功能和令人发指交互之外,在性能上也应该追求丝滑的要求 ...

  4. 基于redis实现tomcat8的tomcat集群的session持久化实现(tomcat-redis-session-manager二次开发)

    前言: 本项目是基于jcoleman的tomcat-redis-session-manager二次开发版本 1.修改了小部分实现逻辑 2.去除对juni.jar包的依赖 3.去除无效代码和老版本tom ...

  5. 分享Ubuntu下一些很棒的软件(一)

    分享一些我在Ubuntu下常用的软件. Goolge Chrome/Firefox/Thunderbird这些重量级的跨平台的软件虽然很强大,但大家应该都比较熟悉了,没有太多必要在这里介绍.本文涉及到 ...

  6. JavaScript-Tool:wdtree

    ylbtech-JavaScript-Tool:wdtree 1.返回顶部 1. 插件描述:wdTree是一个轻量级jQuery插件用于创建一个带有嵌套Check Boxe的树形控件. wdTree是 ...

  7. 【205】C#实现远程桌面访问

    参考:Remote Desktop using C#.NET 参考文件:TscForm.zip 本博客主要是讲述怎样用 .NET 平台中 Microsoft Terminal Services Cli ...

  8. 最优配餐_暴力bfs

    问题描述 栋栋最近开了一家餐饮连锁店,提供外卖服务.随着连锁店越来越多,怎么合理的给客户送餐成为了一个急需解决的问题. 栋栋的连锁店所在的区域可以看成是一个n×n的方格图(如下图所示),方格的格点上的 ...

  9. Codeforces702C【二分】

    题意: 给你几个城市,蜂窝塔量: 给出城市和塔的坐标可以重叠,非递减的方式给出: 输出最小的r,以至于所有的城市能被覆盖到: 思路: 目的就是要使每个城市覆盖到,那我对每个城市找离最近塔的距离,然后在 ...

  10. POJ1017 【据说是贪心...】

    题意: 有6种面积的格子,给出这些格子的数量,然后有6*6的格子去容纳这6种面基,问最少需要几个6*6格子,使得所有类型的小格子被容纳. 思路: 按照面积的从大到小放. 一开始还是太天真,还要用各种1 ...