zoukankan      html  css  js  c++  java
  • 遍历FTP目录及下载

    操作ftp,直接在main方法中即可操作。

    例1:遍历ftp目录中的文件

     1 public static void main(String[] args) throws IOException {
     2 
     3     FTPClient ftpClient = new FTPClient();
     4     
     5     try {
     6 
     7         // 连接FTP服务器
     8         ftpClient.connect("100.100.1.100", 21);//地址,端口号
     9 
    10         // 登录FTP服务器
    11         ftpClient.login("admin", "admin");//用户名,密码
    12 
    13         // 验证FTP服务器是否登录成功
    14         ftpClient.setControlEncoding("UTF-8");
    15 
    16         int replyCode = ftpClient.getReplyCode();
    17         
    18         if (!FTPReply.isPositiveCompletion(replyCode)) {
    19             System.out.println("登录失败");
    20             return;
    21         }else{
    22             System.out.println("登录成功");
    23         }
    24         
    25         //根目录 登录到ftp后,所在的目录即是根目录,直接即可遍历文件
    26         FTPFile[] files = ftpClient.listFiles();
    27         for(FTPFile file: files){
    28             System.out.println(file.getName());
    29         }
    30         
    31         // 切换目录 想要遍历那个目录的文件,先要切换目录(切换目录实际就是进入目录),进到目录之后再进行遍历
    32         if(!ftpClient.changeWorkingDirectory("/transactionDetail")){
    33             System.out.println("切换目录失败");
    34             return;
    35         }else{
    36             FTPFile[] fileDic = ftpClient.listFiles();
    37             for(FTPFile f: fileDic){
    38                 System.out.println(f.getName());
    39             }
    40         }
    41         
    42         ftpClient.logout();
    43 
    44     } catch (IOException e) {
    45         e.printStackTrace();
    46     } catch (Exception e) {
    47         e.printStackTrace();
    48     } finally {
    49         if (ftpClient.isConnected()) {
    50             try {
    51                 ftpClient.logout();
    52             } catch (IOException e) {
    53         
    54             }
    55         }
    56     }
    57 }

    例2:读取(下载)ftp中的文件

     1 public static void main(String[] args) throws IOException {
     2     FTPClient ftpClient = new FTPClient();
     3     OutputStream out = null;
     4     InputStream in = null;
     5     try {
     6 
     7         // 连接FTP服务器
     8         ftpClient.connect("100.100.1.100", 21);
     9 
    10         // 登录FTP服务器
    11         ftpClient.login("admin", "admin");
    12 
    13         // 验证FTP服务器是否登录成功
    14         ftpClient.setControlEncoding("UTF-8");
    15         int replyCode = ftpClient.getReplyCode();
    16         
    17         if (!FTPReply.isPositiveCompletion(replyCode)) {
    18             System.out.println("登录失败");
    19         }else{
    20             System.out.println("登录成功");
    21         }
    22         
    23         // 切换目录
    24         if(!ftpClient.changeWorkingDirectory("/book_detail")){
    25             System.out.println("切换目录失败");
    26         }else{
    27             in = new FileInputStream("detail-2020-02-20-xls.zip");//读取目录中的文件
    28             //创建本地文件
    29             File tmpFile = new File("D:" + File.separator + "route" + File.separator + "detail-2020-02-20-xls.zip");
    30             if (!tmpFile.getParentFile().exists()) {
    31                 tmpFile.getParentFile().mkdirs();//创建目录
    32             }
    33             if(!tmpFile.exists()) {
    34                 tmpFile.createNewFile();//创建文件
    35             }
    36             out = new FileOutputStream(tmpFile);
    37             
    38             // 创建字节数组  
    39             byte[] temp = new byte[1024];
    40             int length = 0;
    41             
    42             // 源文件读取一部分内容  
    43             while ((length = in.read(temp)) != -1) {
    44                 // 目标文件写入一部分内容 
    45                 out.write(temp, 0, length);
    46             }
    47             
    48             in.close();
    49             out.close();
    50             
    51             System.out.println("写入本地成功");
    52         }
    53         
    54         ftpClient.logout();
    55     } catch (IOException e) {
    56         e.printStackTrace();
    57     } catch (Exception e) {
    58         e.printStackTrace();
    59     } finally {
    60         if (ftpClient.isConnected()) {
    61             try {
    62                 ftpClient.logout();
    63             } catch (IOException e) {
    64         
    65             }
    66         }
    67     }
    68 }

    所谓的下载实质上就是输出。

  • 相关阅读:
    cas 单点登录服务端客户端配置
    POI 导出excel
    关于小米手机刷机亲尝
    C#对本地文件重命名--适用于下载的图片、电视剧等奇怪名字的重命名
    泛型List<T>与非泛型ArrayList
    设置一键启动多文件
    网页显示电子表
    插入sql语句01值时,在数据库中的查询时显示为1
    C#面向对象--继承
    SqlServer数据库查询不同字段-年龄段分析
  • 原文地址:https://www.cnblogs.com/jingyi17/p/7766592.html
Copyright © 2011-2022 走看看