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
|
package uploadfileftp;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
public class UploadFileFTP {
public static void main(String args[]) {
// get an ftpClient object
FTPClient ftpClient = new FTPClient();
FileInputStream inputStream = null;
try {
// pass directory path on server to connect
ftpClient.connect("mon.compte.com");
// pass username and password, returned true if authentication is
// successful
boolean login = ftpClient.login("username", "password");
if (login) {
System.out.println("Connection established...");
inputStream = new FileInputStream("files/fileToUpload.txt");
boolean uploaded = ftpClient.storeFile("uploadedFile.txt",
inputStream);
if (uploaded) {
System.out.println("File uploaded successfully !");
} else {
System.out.println("Error in uploading file !");
}
// logout the user, returned true if logout successfully
boolean logout = ftpClient.logout();
if (logout) {
System.out.println("Connection close...");
}
} else {
System.out.println("Connection fail...");
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} |
Partager