honey52160
 1 package com.cap.crm.imp;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 import java.io.DataInputStream;
 6 import java.io.File;
 7 import java.io.IOException;
 8 import java.text.ParseException;
 9 import java.text.SimpleDateFormat;
10 import java.util.ArrayList;
11 import java.util.Calendar;
12 import java.util.Date;
13 import java.util.List;
14 
15 import org.apache.commons.net.ftp.FTPClient;
16 import org.apache.commons.net.ftp.FTPFile;
17 import org.apache.commons.net.ftp.FTPReply;
18 
19 
20 public class test {
21     
22     /**
23      * @param args
24      */
25     public static void main(String[] args) {
26 
27         try {
28             List<String> list = new ArrayList<String>();
29 
30             Connection conn = new Connection("xxxx.xxxxx.xxxxx"); //FTP服务器地址
31             conn.connect();
32             boolean isAuthenticated = conn.authenticateWithPassword("用户名","密码"); //系统用户
33             if(isAuthenticated==false){
34                 throw new IOException("faild");
35             }
36             SCPClient client = new SCPClient(conn);
37             
38 
39             //建立连接  FTP用户
40             FTPClient ftp = new FTPClient();
41             ftp.connect("xxxxx.xxxxx.xxxx.xxxx");//FTP服务器地址
42             ftp.login("FTP用户名", "FTP用户密码"); //FTP用户名密码
43             int reply = ftp.getReplyCode();
44             if(!FTPReply.isPositiveCompletion(reply)){
45                 //ftp.disconnect();
46             }
47             Date date = new Date();
48             SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
49             Calendar calendar = Calendar.getInstance();
50             calendar.setTime(date);
51             calendar.add(Calendar.DAY_OF_MONTH, -1);
52             date = calendar.getTime();
53             String lastDate = format.format(date);
54 //取当前日期的前一天
55             ftp.setControlEncoding("GBK");
56             ftp.enterLocalPassiveMode();
57             FTPFile[] fs = ftp.listFiles(""); ////ocrmweb//app//logs//otip  这里可以不写路径
58             //String[] ns = ftp.listNames("//ocrmweb//app//logs//otip");
59             //System.out.println(fs.length);
60             //System.out.println(ns.length);
61             for (FTPFile file : fs) {
62                 if((file.getName()).endsWith(".log")&&(file.getName()).contains(lastDate)){
63                 StringBuffer url = new StringBuffer();
64                     url.append("/log/log/").append(file.getName());
65                 list.add(url.toString());
66                 }
67             }
68             System.out.println(list.size());
69             for (String string : list) {
70                 System.out.println(string);
71                 client.get(string,"D:\\test\\slow_log_report\\"); //写入本地路径
72             }
73             ftp.logout();
74             
75         } catch (Exception e) {
76             
77         }
78     }
79 
80 }

通过以上代码,即可实现FTP下载指定目录指定日期的文件到本地

需要加载的JAR出jdk自带jar包外,还需要准备ganymed-ssh2-build210.jar、、、、、

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2021-06-10
猜你喜欢
  • 2021-12-04
  • 2022-01-02
  • 2022-02-09
  • 2021-08-22
  • 2021-12-06
  • 2021-11-13
  • 2022-12-23
相关资源
相似解决方案