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
|
public class EnvoieFTP extends Thread {
private FtpConfigurationParameters ftpConfigurationParameters;
private String fileNameToUpload;
private String path = "D:/TestWebservice/temp/";
public EnvoieFTP(FtpConfigurationParameters ftpConfigurationParameters, String filNameToUpload) {
this.fileNameToUpload = filNameToUpload;
this.ftpConfigurationParameters = ftpConfigurationParameters;
this.setDaemon(true);
this.start();
}
@Override
public void run() {
FTPClient client = new FTPClient();
client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
String host = "192.168.2.144";
String username = "plopinou";
String password = "plopinou";
FileInputStream in = null;
try {
this.sleep(5000);
client.connect(host);
int reply = client.getReplyCode();
System.out.println("Reply code for connection : " + reply);
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
System.err.println("FTP server refused connection.");
}
client.login(username, password);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.enterLocalPassiveMode();
path = path + fileNameToUpload;
in = new FileInputStream(new File(path));
System.out.println("FILE TO UPLOAD : " + fileNameToUpload);
client.storeFile(fileNameToUpload, in);
System.out.println("Reply code for storefile : " + client.getReplyCode());
in.close();
} catch (SocketException ex) {
System.out.println("exception SocketException : " + client.getReplyCode());
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("Exception : " + client.getReplyCode());
ex.printStackTrace();
} catch (InterruptedException ex) {
System.out.println(ex.getMessage());
} finally {
try {
client.disconnect();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
} |