Bonjour,

Je me permet de poster car j'ai un soucis et peut-être que vous pourrez m'aider !
Pour un projet, on m'a demander d'établir une connection FTP en Java afin de download des fichiers (Pour cela il n'y a pas de problème). On m'a demandé que le programme récupère toutes les heures fichiers (car ils subiront une modification régulière). J'ai donc pour cela décié d'utiliser Quartz et plus particulièrement Cron Schedule.
Le problème est donc le tout ensemble. Je vous montre mon code pour que vous puissiez voir (je pense que j'utilise mal mon Job mais comme je ne connais pas du tout cette API j'ai du mal à comprendre)

Quartz.java

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.auscult.ftp;
import java.io.IOException;
import java.util.TimeZone;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
 
public class Quartz {
 public static void main(final String[] args) {
     final SchedulerFactory factory = new StdSchedulerFactory();
     Scheduler scheduler = null;
     try {
       scheduler = factory.getScheduler();
       final JobDetail jobDetail = JobBuilder
           .newJob(FTPFunctions.class)
           .withIdentity("monJob", "groupe_1" )
           .usingJobData("monParametre", "12345" )
           .build();
       final Trigger cronTrigger = TriggerBuilder
           .newTrigger()
           .withIdentity("monTrigger", "groupe_1" )
           .withSchedule(
               CronScheduleBuilder.cronSchedule("0 0/1 * * * ?" ) //Interval de 1mn afin de faire des test)
                 .inTimeZone(TimeZone.getTimeZone("Europe/Paris" )))
           .build();
       scheduler.start();
       scheduler.scheduleJob(jobDetail, cronTrigger);
       System.in.read();
       if (scheduler != null) {
         scheduler.shutdown();
       }
     } catch (final SchedulerException e) {
       e.printStackTrace();
     } catch (final IOException e) {
       e.printStackTrace();
     }
   }
}

FTPFunctions.java

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.auscult.ftp;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
public class FTPFunctions implements Job {
 
private String host;
private String username;
private String password;
private int port;
private static String remoteFilePath;
private static String savePath;
private static String parentDir;
private static String currentDir;
private static String saveDir;
private static FTPClient ftpClient = new FTPClient();
 
 public FTPFunctions(String server, int pPort, String pUsername, String pPassword) throws Exception {
 	host = server;
 	port = pPort;
 	username = pUsername;
 	password = pPassword;
 	ftpClient.addProtocolCommandListener (new PrintCommandListener(new PrintWriter(System.out)));
        int reply;
        ftpClient.connect( host , port);
        System.out.println("FTP URL is:"+ftpClient.getDefaultPort());
        reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            throw new Exception("Exception in connecting to FTP Server" );
        }
        ftpClient.login(username, password);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();     
 
 }
 
 public static boolean downloadFTPFile(FTPClient ftpClient, String RemoteFilePath, String SavePath) throws IOException {
 	remoteFilePath  = RemoteFilePath;
 	savePath    = SavePath;
 	File downloadFile = new File(savePath);
 
     File parentDir = downloadFile.getParentFile();
     if (!parentDir.exists()) {
         parentDir.mkdir();
     }
 
     OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
 
     try {
 
         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
         return ftpClient.retrieveFile(remoteFilePath, outputStream);
     } catch (IOException ex) {
         throw ex;
     } finally {
         if (outputStream != null) {
             outputStream.close();
         }
     }
    }
 
 public static void downloadDirectory(FTPClient ftpClient, String ParentDir, String CurrentDir, String SaveDir) throws IOException {
 	parentDir  = ParentDir;
 	currentDir = CurrentDir;
 	saveDir    = SaveDir;
     String dirToList = parentDir;
     if (!currentDir.equals("" )) {
         dirToList += "/" + currentDir;
     }
     FTPFile[] subFiles = ftpClient.listFiles(dirToList);
     if (subFiles != null && subFiles.length > 0) {
         for (FTPFile aFile : subFiles) {
             String currentFileName = aFile.getName();
             if (currentFileName.equals("." ) || currentFileName.equals(".." )) {
                 continue;
             }
             String filePath = parentDir + "/" + currentDir + "/"
                     + currentFileName;
             if (currentDir.equals("" )) {
                 filePath = parentDir + "/" + currentFileName;
             }
             String newDirPath = saveDir + parentDir + File.separator + currentDir + File.separator + currentFileName;
             if (currentDir.equals("" )) {
                 newDirPath = saveDir + parentDir + File.separator
                           + currentFileName;
             }
             if (aFile.isDirectory()) {
                 File newDir = new File(newDirPath);
                 boolean created = newDir.mkdirs();
                 if (created) {
                     System.out.println("CREATED the directory: " + newDirPath);
                 } else {
                     System.out.println("COULD NOT create the directory: " + newDirPath);
                 }
                 downloadDirectory(ftpClient, dirToList, currentFileName, saveDir);
             } else {
                 boolean success = downloadFTPFile(ftpClient, filePath, newDirPath);
                 if (success) {
                     System.out.println("DOWNLOADED the file: " + filePath);
                 } else {
                     System.out.println("COULD NOT download the file: " + filePath);
                 }
             }
         }
     }
 }
 
 public void disconnect(){
        if (FTPFunctions.ftpClient.isConnected()) {
             try {
                 FTPFunctions.ftpClient.logout();
                 FTPFunctions.ftpClient.disconnect();
             } catch (IOException f) {
             }
         }
    }
 
public void execute(JobExecutionContext arg0) throws JobExecutionException {
                System.out.println("FTP execut" );
 	try {
   FTPFunctions ftp = new FTPFunctions("Test", 21, "killkala", "test" );
   String remoteDirPath = "/Station1";
            String saveDirPath = "//192.168.7.191/web/FTP";
 
            FTPFunctions.downloadDirectory(ftpClient, remoteDirPath, "", saveDirPath);
   System.out.println("FTP File downloaded successfully" );
   ftp.disconnect();
    } catch (Exception e) {
     e.printStackTrace();
    }     	 
}
}
Je pense que mon Job n'aime pas trop toutes mes fonctions privées & public plus haut. En faite le problème c'est que je n'ai aucune erreur, juste rien ne se passe dans ma console. J'ai fais des test dépendant pour essayer de comprendre et c'est pour cela que j'en suis arrivé à la conclusion que l'association de mon Job à ma classe FTPFunctions n'a pas été concluante !
Je pense que ca dois être une erreur trop bete, mais j'ai pas encore assez de connaissances pour savoir !
Donc si jamais vous avez un peu de temps pour m'aider, ca serai avec grand plaisir !!!

Merci d'avance pour vos réponses !