FTP client - listfiles - java

I am unable to get the exact file list using FTPClient. Sample code as below :
FTPClient client = new FTPClient();
client.connect("x.x.x.x");
client.login("abcd", "abcd");
FTPFile[] ftpFiles = client.listFiles();
for (FTPFile ftpFile : ftpFiles) {
System.out.println("FTPFile: " + ftpFile.getName());
}
I tried to set to PASV mode using enterLocalPassiveMode()/enterRemotePassiveMode()/pasv(). But, it doesnt work.
Please also check Apache Commons FTPClient.listFiles ..
Thank you

I don't know what files is, but you're getting the results of client.listFiles in ftpFiles, and not in files. Then in your for loop you go over files.

Try this.
String[] fileFtp = client.listNames();//if it is directory. then list of file names
//download file
for (int i =0;i<fileFtp.length;i++) {
String fileName = fileFtp[i];
OutputStream out = new FileOutputStream(new File("local temp file name"));
if (!client.retrieveFile(fileName, out)) {
sysout("Could not download the file. "+ fileName);
} else {
sysout("Downloaded file # : "+localFileName);
}
}
This should work.
Thanks.

Related

Java Apache FTPClient most downloaded files are empty or missing

This is my code that is supposed to download entire FTP directory to a local folder. It does it well, but most of the files are 0KB in size. Only JSON files seem to contain all their data.
Things I tried:
Changing FTP file type with client.setFileType("FTP.BINARY_FILE_TYPE");
Using OutputStream instead of FileOutputStream
Code:
public static void copyFolder(File destination, FTPFile sourceFile, FTPClient ftpClient) throws IOException{
if (!sourceFile.isDirectory()) {
//copy file
File downloadFile = new File(destination + "/"+ sourceFile.getName());
String remoteFile = sourceFile.getName();
FileOutputStream outputStream = new FileOutputStream(downloadFile);
System.out.println(remoteFile);
System.out.println(downloadFile.getPath());
boolean success = ftpClient.retrieveFile(remoteFile, outputStream);
if(success) {
System.out.println("Retrieved " + remoteFile);
}
outputStream.close();
}else{
//loop through a subdirectory
ftpClient.changeWorkingDirectory(ftpClient.printWorkingDirectory() + "/" + sourceFile.getName());
System.out.println(ftpClient.printWorkingDirectory());
FTPFile[] contents = ftpClient.listFiles(ftpClient.printWorkingDirectory());
File newDest = new File(destination + "/" + sourceFile.getName());
if(!newDest.exists()){
newDest.mkdir();
}
for(FTPFile file : contents){
copyFolder(newDest, file, ftpClient);
}
return;
}
}
How to get the transfer correctly?
log from ftp
tree of the ftp directory
tree of downloading directory
Trying to download it on the same computer ended with losing connection a few times - between and during file downloads. Also it seems that few files are downloaded. I will change the title of question to be more specific.
Only two files are being copied for some reason – https://pastebin.com/XNWqRMDj They are not empty.
The problem is your changeWorkingDirectory call. It's failing most of the time.
ftpClient.changeWorkingDirectory(ftpClient.printWorkingDirectory() + "/" + sourceFile.getName());
It should be:
ftpClient.changeWorkingDirectory(destination + "/" + sourceFile.getName());
For a complete working code for downloading FTP folders in Java, see:
Download all folders recursively from FTP server in Java

Not able to access file in eclipse gives : FileNotFoundException

In eclipse i am trying to give path of one configuration.json file but it is not taking it .
System.getProperty("user.dir")
gives C:\Users\cmalik\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse
and
try {
File f = new File("."); // current directory
File[] files = f.listFiles();
for (File file : files) {
if (file.isDirectory()) {
System.out.print("directory:");
} else {
System.out.print(" file:");
}
System.out.println(file.getCanonicalPath());
}
gives
file:C:\Users\cm\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse\Eclipse Jee Neon.lnk
file:C:\Users\cm\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse\hs_err_pid10180.log
file:C:\Users\cm\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse\hs_err_pid9728.log
And my project structure is
ProjectName
---src
---- com.abc.def.ghi.jkl.WebServices
------ myService.java
---configuration.json
I tried code for accessing file is :
FileReader file = new FileReader("configuration.json");
or
FileReader file = new FileReader("projectName\\configuration.json");
It is not able to access as output for System.getProperty("user.dir") is eclipse folder not my current projectName folder.
How can i get my files please let me know.
You can use either of the below two:
String location=System.getProperty("user.dir");
or
String location= new java.io.File(".").getCanonicalPath();
And then try to access the file using:
FileReader file = new FileReader(location+"\\configuration.json");
Hope it helps.

Find files with matching directory and filename

I currently have to find files with these pattern and work on the, but I cannot find anything that would help me match the full directory + the filename of the file I need to explore
Here's an example of full directory that I need to work with.
/logs/xxx/production/jboss/instance*/xxx-production-zzzz*/xxx-production-zzzz*-XXX-Metrics.log
Thanks in advance.
Best solution found for this case.
Had to import the ant.jar from apache.
logs = new ArrayList<File>();
String basedir = "/logs/xxx/production/jboss";
String[] include = {"pvmk*/xxx-production-zzzzz*/xxx-production-zzzzz*-XXX-Metrics.log"};
System.out.println(new Timestamp(System.currentTimeMillis()));
System.out.println("Reading files...");
DirectoryScanner scanner = new DirectoryScanner();
scanner.setIncludes(include);
scanner.setBasedir(basedir);
scanner.setCaseSensitive( true );
scanner.scan();
String[] files = scanner.getIncludedFiles();
for (String file : files) {
System.out.println("Loading file: " + file );
File log;
log = new File(new String ("/logs/xxx/production/jboss/" + file));
logs.add(log);
}

Add suffix to filename if file already exist instead of overwriting it

I'm saving an uploaded file as below:
UploadItem item = event.getUploadItem();
File dir = new File("D:/FileUpload");
if (!dir.exists()) {
dir.mkdir();
}
File bfile = new File("D:/FileUpload" + "/" + item.getFileName());
OutputStream outStream = new FileOutputStream(bfile);
outStream.write(item.getData());
outStream.close();
But my question is when upload once file same old file in folder D:/FileUpload. In above function it will delete old file. Example first time, i upload file : test.doc (old file). Then i upload another file with same name : test.doc (new file). At folder FileUpload will has one file is test.doc (new file). I want function will process similar in window OS is : new file will be test (2).doc. How can i process it ? And all cases : D:/FileUpload have many file : test.doc, test (1).doc, test (2).doc, test (a).doc,...... I think we just check with format ....(int).doc. That new file will be :
test (3).doc (ignore test(a).doc)
Maybe you're looking for something like this? I list the files in your directory, compare the names of each to the name of your file. If the names match, increment a count. Then, when you come to create your file, include the count in its name.
UploadItem item = event.getUploadItem();
File dir = new File("D:/FileUpload");
if (!dir.exists()) {
dir.mkdir();
}
String [] files = dir.list();
int count = 0;
for(String file : files) {
if (file.startsWith(item.getFileName()) {
count++;
}
}
File bfile = new File("D:/FileUpload" + "/" + item.getFileName() + "(" + count + ")");
OutputStream outStream = new FileOutputStream(bfile);
outStream.write(item.getData());
outStream.close();

How to convert FTPFIle to File using Java?

I need to read all the files from a shared location and returns a File Map. I use FTPClient to access the shared location. Using FTPClient I able to retrieve all the File as a FTPFile. But I want Convert FTPFile to File. please see the code.
FTPFile[] ftpFiles = ftpClient.listFiles(folderPath);
Note:- I Don't want to Create new connection every time. I want to read all in one connection
Looks like this is very old question but just wanted to update what I have done.
InputStream iStream=ftpClient.retrieveFileStream(ftpFile.getName());
File file = File.createTempFile("tmp", null);
FileUtils.copyInputStreamToFile(iStream, file);
Hopefully this is helpful.
If you only want get the name, try this code:
private File[] getRemoteFilesInFolder() {
FTPFile[] elements;
File[] files;
try {
elements = ftpClient.listFiles();
files = new File[elements.length];
for(int i=0; i< elements.length; i++) {
files[i] = new File(elements[i].getName());
}
return files;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

Categories