javalogo

Delete file in FTP Server Using Java


How to delete file in FTP Server Using Java ?

Reference : here

 

package file.ftp.delete;

import java.io.FileInputStream;

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPFile;

 

public class DeleteFileFTP {

public static void main(String[] args) {

FTPClient ftpClient = new FTPClient();

String hostname = “[Path for access FTP Server]”;

int port = 21;

String user = “[user]”;

String pass = “[password]”;

try {

ftpClient.connect(hostname, port);

boolean login = ftpClient.login(user, pass);

if(login){

System.out.println(“Connection established…”);

FTPFile[] files = ftpClient.listFiles(“Current/”); //directory from FTP server 

for (FTPFile file : files) {

if (file.isFile()) {

System.out.println(file.getName());

boolean deleted = ftpClient.deleteFile(“Current/”+file.getName());

if (deleted) {

System.out.println(“File sudah dihapus.”);

} else {

System.out.println(“File tidak bisa dihapus.”);

}

}

}

}

} catch (Exception e) {

e.printStackTrace();

System.out.println(e.getMessage());

}finally{

try {

if (ftpClient.isConnected()) {

boolean logout = ftpClient.logout();

ftpClient.disconnect();

if (logout) {

System.out.println(“Connection close…”);

} else {

System.out.println(“Connection fail…”);

}

}

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s