How to delete all txt files inside an FTP directory? - java

My app stores txt files on a FTP server which are also hosted on webservice.
In the directory where I host txt files I can find other txt files. I would like to delete all files in current directory every time when I store the new ones.
Actually i was trying to use the following command:
FTPClient ftpClient = new FTPClient();
ftpClient.connect(siteFTP);
if (ftpClient.login(usrFTP, pswFTP)) {
ftpClient.enterLocalPassiveMode();
FTPFile[] remoteFiles = ftpClient.listFiles(path);
if (remoteFiles.length > 0) {
ftpClient.deleteFile("/prenotazioni/*.txt");
}
}
But even if there was txt files in that directory, FTP respose is:
> DELE /prenotazioni/*.txt
> 550 File not found

Using * won't work. After You get list of files in declared directory, You must iterate it and delete files one by one by using deleteFile(String pathname) (also checking if file name endsWith(".txt")).
Every FTPFile has method getName(). You should construct full path so FTPClient will know what file to delete. I believe it would be something like:
ftpClient.deleteFile("/prenotazioni/" + remoteFiles[i].getName());

full method:
public static void deleteFilesInFolderFtp(String dirPath, FTPClient ftpClient) {
try {
// use local passive mode to pass firewall
ftpClient.enterLocalPassiveMode();
FTPFile[] remoteFiles = ftpClient.listFiles("/" + dirPath);
if (remoteFiles.length > 0) {
for (int i = 0; i < remoteFiles.length; i++) {
ftpClient.deleteFile("/" + dirPath + "/" + remoteFiles[i].getName());
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}

Related

checking a file starts with a name in ftp location using java

Before downloading a file needs check whether the file(file name starts) exist or not:
I had a ftp location, it will generate a file in response to hitting a service(API). I need to check whether file exits or not in ftp location using a starting characters of file name because it will append some data at end of file name.
Can any one help on this using java code with commons.net package
Use FTPFileFilter
try {
String filePattern = "prefix";
FTPClient objFTPClient = new FTPClient();
//objFTPClient - set username, password, host, etc...
FTPFileFilter ftpFileFilter = new FTPFileFilter() {
#Override
public boolean accept(FTPFile ftpFile) {
return ftpFile.getName().toLowerCase().startsWith(filePattern.toLowerCase());
}
};
/* List of file that starts with your given prefix */
FTPFile[] ftpFiles = objFTPClient.listFiles(remoteDirectory, ftpFileFilter);
}catch(Exception ex){
ex.printStackTrace();
}finally{
//close connection, etc....
}

How to read a folder in the network (shared folder) from the java code

I am trying to read a shared folder from an Java method . The below method works fine with the folder in the local machine but the same code will not work with the shared folder in the network . It throws a null pointer exception when tried to read a shared folder . can any body pls tell me a solution to read a shared folder in the network .
as suggested i have tried converting file name to URI even that does not work
public static void listFiles() {
String fileName = "file://**.40.10.**/B2BAddOn/Orders_POC";
File folder = new File(fileName);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
}
If it is at same domain u can.But connection to remote server requires something additionals.May be it needs authentication.There are different methods.
U can use smbfile and jcifs .
Another way you can use httpserver at remote side to acess files.

URLConnection FTP list files

URL url = new URL("ftp://user:pass#ftp.example.com/thefolder/");
URLConnection connection = url.openConnection();
...
// List files in folder...
Using something like the above, I was wondering how I could grab a list of files within folder 'thefolder'?
Following on from this original question, I have put together this simple FTP connection which is all working and looking good. It can see all files in the /live/conf/ location and copies them all to the local /conf/ location.
The only issue is, it is copying the files but there's no content.They are all 0KB and empty.
Can anyone see anything obvious that'd be copying the filename but not file content?
try {
FTPClient ftp = new FTPClient();
ftp.connect("000.000.000.000");
ftp.login("USER", "PASSWORD");
ftp.enterLocalPassiveMode();
ftp.setFileType(FTP.BINARY_FILE_TYPE);
FTPFile[] files = ftp.listFiles("/live/conf/");
for (int i=0; i < files.length; i++) {
if (files[i].getName().contains(".csv")) {
String remoteFile1 = files[i].getName();
File downloadFile1 = new File("/var/local/import/conf/"+files[i].getName());
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
ftp.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
}
}
ftp.disconnect();
} catch (SocketException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
The Java SE URLConnection is insuitable for the job of retrieving a list of files from a FTP host. As to FTP, it basically only supports the FTP get or put commands (retrieve or upload file). It does not support the FTP ls command (list files) which you're basically looking for, let alone many others.
You need to look for 3rd party libraries supporting the FTP ls command (and many more). A commonly used one is the Apache Commons Net FtpClient. In its javadoc is demonstrated how to issue a ls:
FTPClient f = new FTPClient();
f.connect(server);
f.login(username, password);
FTPFile[] files = f.listFiles(directory);
You could use Apache commons FTPClient
This would allow you to call listFiles with...
public static void main(String[] args) throws IOException {
FTPClient client = new FTPClient();
client.connect("c64.rulez.org");
client.enterLocalPassiveMode();
client.login("anonymous", "");
FTPFile[] files = client.listFiles("/pub");
for (FTPFile file : files) {
System.out.println(file.getName());
}
Check out this class I found. It's does the lifting for you.
Class at nsftools.com
Example would be:
FTPConnection ftpConnect = new FTPConnection();
ftpConnect.connect("ftp.example.com");
ftpConnect.login("user","pass");
System.out.println(ftpConnect.listFiles());

How to copy a file on the FTP server to a directory on the same server in Java?

I'm using Apache Commons FTP to upload a file. Before uploading I want to check if the file already exists on the server and make a backup from it to a backup directory on the same server.
Does anyone know how to copy a file from a FTP server to a backup directory on the same server?
public static void uploadWithCommonsFTP(File fileToBeUpload){
FTPClient f = new FTPClient();
FTPFile backupDirectory;
try {
f.connect(server.getServer());
f.login(server.getUsername(), server.getPassword());
FTPFile[] directories = f.listDirectories();
FTPFile[] files = f.listFiles();
for(FTPFile file:directories){
if (!file.getName().equalsIgnoreCase("backup")) {
backupDirectory=file;
} else {
f.makeDirectory("backup");
}
}
for(FTPFile file: files){
if(file.getName().equals(fileToBeUpload.getName())){
//copy file to backupDirectory
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
Edited code: still there is a problem, when i backup zip file, the backup-ed file is corrupted.
Does any body know the reason for it?
public static void backupUploadWithCommonsFTP(File fileToBeUpload) {
FTPClient f = new FTPClient();
boolean backupDirectoryExist = false;
boolean fileToBeUploadExist = false;
FTPFile backupDirectory = null;
try {
f.connect(server.getServer());
f.login(server.getUsername(), server.getPassword());
FTPFile[] directories = f.listDirectories();
// Check for existence of backup directory
for (FTPFile file : directories) {
String filename = file.getName();
if (file.isDirectory() && filename.equalsIgnoreCase("backup")) {
backupDirectory = file;
backupDirectoryExist = true;
break;
}
}
if (!backupDirectoryExist) {
f.makeDirectory("backup");
}
// Check if file already exist on the server
f.changeWorkingDirectory("files");
FTPFile[] files = f.listFiles();
f.changeWorkingDirectory("backup");
String filePathToBeBackup="/home/user/backup/";
String prefix;
String suffix;
String fileNameToBeBackup;
FTPFile fileReadyForBackup = null;
f.setFileType(FTP.BINARY_FILE_TYPE);
f.setFileTransferMode(FTP.BINARY_FILE_TYPE);
for (FTPFile file : files) {
if (file.isFile() && file.getName().equals(fileToBeUpload.getName())) {
prefix = FilenameUtils.getBaseName(file.getName());
suffix = ".".concat(FilenameUtils.getExtension(file.getName()));
fileNameToBeBackup = prefix.concat(Calendar.getInstance().getTime().toString().concat(suffix));
filePathToBeBackup = filePathToBeBackup.concat(fileNameToBeBackup);
fileReadyForBackup = file;
fileToBeUploadExist = true;
break;
}
}
// If file already exist on the server create a backup from it otherwise just upload the file.
if(fileToBeUploadExist){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
f.retrieveFile(fileReadyForBackup.getName(), outputStream);
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
if(f.storeUniqueFile(filePathToBeBackup, is)){
JOptionPane.showMessageDialog(null, "Backup succeeded.");
f.changeWorkingDirectory("files");
boolean reply = f.storeFile(fileToBeUpload.getName(), new FileInputStream(fileToBeUpload));
if(reply){
JOptionPane.showMessageDialog(null,"Upload succeeded.");
}else{
JOptionPane.showMessageDialog(null,"Upload failed after backup.");
}
}else{
JOptionPane.showMessageDialog(null,"Backup failed.");
}
}else{
f.changeWorkingDirectory("files");
f.setFileType(FTP.BINARY_FILE_TYPE);
f.enterLocalPassiveMode();
InputStream inputStream = new FileInputStream(fileToBeUpload);
ByteArrayInputStream in = new ByteArrayInputStream(FileUtils.readFileToByteArray(fileToBeUpload));
boolean reply = f.storeFile(fileToBeUpload.getName(), in);
System.out.println("Reply code for storing file to server: " + reply);
if(!f.completePendingCommand()) {
f.logout();
f.disconnect();
System.err.println("File transfer failed.");
System.exit(1);
}
if(reply){
JOptionPane.showMessageDialog(null,"File uploaded successfully without making backup." +
"\nReason: There wasn't any previous version of this file.");
}else{
JOptionPane.showMessageDialog(null,"Upload failed.");
}
}
//Logout and disconnect from server
in.close();
f.logout();
f.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
If you are using apache commons net FTPClient, there is a direct method to move a file from one location to another location (if the user has proper permissions).
ftpClient.rename(from, to);
or, If you are familiar with ftp commands, you can use something like
ftpClient.sendCommand(FTPCommand.yourCommand, args);
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
//command successful;
} else {
//check for reply code, and take appropriate action.
}
If you are using any other client, go through the documentation, There wont be much changes between client implementations.
UPDATE:
Above approach moves the file to to directory, i.e, the file won't be there in from directory anymore. Basically ftp protocol meant to be transfer the files from local <-> remote or remote <-> other remote but not to transfer with in the server.
The work around here, would be simpler, get the complete file to a local InputStream and write it back to the server as a new file in the back up directory.
to get the complete file,
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ftpClient.retrieveFile(fileName, outputStream);
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
now, store this stream to backup directory. First we need to change working directory to backup directory.
// assuming backup directory is with in current working directory
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//binary files
ftpClient.changeWorkingDirectory("backup");
//this overwrites the existing file
ftpClient.storeFile(fileName, is);
//if you don't want to overwrite it use storeUniqueFile
Hope this helps you..
Try this way,
I am using apache's library .
ftpClient.rename(from, to) will make it easier, i have mentioned in the code below
where to add ftpClient.rename(from,to).
public void goforIt(){
FTPClient con = null;
try
{
con = new FTPClient();
con.connect("www.ujudgeit.net");
if (con.login("ujud3", "Stevejobs27!!!!"))
{
con.enterLocalPassiveMode(); // important!
con.setFileType(FTP.BINARY_FILE_TYPE);
String data = "/sdcard/prerakm4a.m4a";
ByteArrayInputStream(data.getBytes());
FileInputStream in = new FileInputStream(new File(data));
boolean result = con.storeFile("/Ads/prerakm4a.m4a", in);
in.close();
if (result)
{
Log.v("upload result", "succeeded");
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$Add the backup Here$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
// Now here you can store the file into a backup location
// Use ftpClient.rename(from, to) to place it in backup
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$Add the backup Here$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
}
con.logout();
con.disconnect();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
There's no standard way to duplicate a remote file over FTP protocol. Some FTP servers support proprietary or non-standard extensions for this though.
So if your are lucky that your server is ProFTPD with mod_copy module, you can use FTP.sendCommand to issue these two commands:
f.sendCommand("CPFR sourcepath");
f.sendCommand("CPTO targetpath");
The second possibility is that your server allows you to execute arbitrary shell commands. This is even less common. If your server supports this you can use SITE EXEC command:
SITE EXEC cp -p sourcepath targetpath
Another workaround is to open a second connection to the FTP server and make the server upload the file to itself by piping a passive mode data connection to an active mode data connection. Implementation of this solution (in PHP though) is shown in FTP copy a file to another place in same FTP.
If neither of this works, all you can do is to download the file to a local temporary location and re-upload it back to the target location. This is that the answer by #RP- shows.
See also FTP copy a file to another place in same FTP.
To backup at same Server (move), can you use:
String source="/home/user/some";
String goal ="/home/user/someOther";
FTPFile[] filesFTP = cliente.listFiles(source);
clientFTP.changeWorkingDirectory(goal); // IMPORTANT change to final directory
for (FTPFile f : archivosFTP)
{
if(f.isFile())
{
cliente.rename(source+"/"+f.getName(), f.getName());
}
}

Recursively list files and directories in an FTP server using Java

I'm currently using a Java FTP library (ftp4j) to access a FTP server. I want to do a file count and directory count for the server, but this means I would need to list files within directories within directories within directories, etc.
How is this achievable? Any hints would be greatly appreciated.
Extract from the code:
client = new FTPClient();
try {
client.connect("");
client.login("", "");
client.changeDirectory("/");
FTPFile[] list = client.list();
int totalDIRS = 0;
int totalFILES = 0;
for (FTPFile ftpFile : list) {
if (ftpFile.getType() == FTPFile.TYPE_DIRECTORY) {
totalDIRS++;
}
}
message =
"There are currently " + totalDIRS + " directories within the ROOT directory";
client.disconnect(true);
} catch (Exception e) {
System.out.println(e.toString());
}
Make a recursive function that given a file that might be a directory returns the number of files and directories in it. Use isDir and listFiles.
Try using recursive functions.
This is could be a function that checks a directory for files, then you can do a check if a file has a child, that would be a directory.
If it has a child you can call that same function again for that directory, etc.
Like this pseudo java here:
void Function(String directory){
... run through files here
if (file.hasChild())
{
Function(file.getString());
}
}
I'm sure you can use that kind of coding in counting the files as well...
Just use a recursive function like below.
Note that my code uses Apache Commons Net, not ftp4j, what the question is about. But the API is pretty much the same and ftp4j seems to be an abandoned project now anyway.
private static void listFolder(FTPClient ftpClient, String remotePath) throws IOException
{
System.out.println("Listing folder " + remotePath);
FTPFile[] remoteFiles = ftpClient.listFiles(remotePath);
for (FTPFile remoteFile : remoteFiles)
{
if (!remoteFile.getName().equals(".") && !remoteFile.getName().equals(".."))
{
String remoteFilePath = remotePath + "/" + remoteFile.getName();
if (remoteFile.isDirectory())
{
listFolder(ftpClient, remoteFilePath);
}
else
{
System.out.println("Foud remote file " + remoteFilePath);
}
}
}
}

Categories

Resources