FTP multiple files using apache commons into a local directory - java

I am trying to download All files in a directory to my local machine using apache commons like this:
import java.io.FileOutputStream;
import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPFile;
public class FTPExample {
public static void main(String[] args) throws SocketException, IOException {
FTPClient client = new FTPClient();
client.connect("MyHostName");
client.enterLocalPassiveMode();
client.login("username", "password");
FTPFile[] files = client.listFiles("/App/");
for (FTPFile file : files) {
System.out.println(file.getName());
FileOutputStream fos = new FileOutputStream("Ftp Files/" + file.getName());
client.retrieveFile(file.getName(),fos);
}
}
}
Am able to list the Files in the Directory but I am Getting FilenotFound Exception when trying to Download the files. Please help.
My Error is:
Exception in thread "main" java.io.FileNotFoundException: Ftp Files\01 (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
at ftpexample.FTPExample.main(FTPExample.java:30)
Java Result: 1
EDIT: I need the files to be stored in the Folder Ftp File/ in their original file names.

Thank you to those who tried to help. I found the answer to my problem here. this is How I did it:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FTPExample {
public static void main(String[] args) {
try {
//new ftp client
FTPClient ftp = new FTPClient();
//try to connect
ftp.connect("MyHhostName");
//login to server
if (!ftp.login("username", "password")) {
ftp.logout();
}
int reply = ftp.getReplyCode();
//FTPReply stores a set of constants for FTP reply codes.
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
//enter passive mode
ftp.enterLocalPassiveMode();
//get system name
System.out.println("Remote system is " + ftp.getSystemType());
//change current directory
ftp.changeWorkingDirectory("/App/PMIGENV/BACK/Finacle/FC/app/CDCI_LOGS/log/UBSADMIN");
System.out.println("Current directory is " + ftp.printWorkingDirectory());
//get list of filenames
FTPFile[] ftpFiles = ftp.listFiles();
if (ftpFiles != null && ftpFiles.length > 0) {
//loop thru files
for (FTPFile file : ftpFiles) {
if (!file.isFile()) {
continue;
}
System.out.println("File is " + file.getName());
//get output stream
OutputStream output;
output = new FileOutputStream("FtpFiles" + "/" + file.getName());
//get the file from the remote system
ftp.retrieveFile(file.getName(), output);
//close output stream
output.close();
//delete the file
// ftp.deleteFile(file.getName());
}
}
ftp.logout();
ftp.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

Here is the Working Code . i Have been trying it for a long time but now it's working fine.
Previously it was downloading files with 0Kb size.
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class FTPDownload {
public void ftpDownload() throws IOException
{
String host="your_host_name";
String uname="your_user_name";
String pass="your_password";
String remoteDIR="/public_html/tmp";
String localDIR="D://FTP";
//Object for FTPClient class
FTPClient ftp=new FTPClient();
ftp.connect(host);
boolean login=ftp.login(uname, pass);
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory(remoteDIR);
FTPFile[] files=ftp.listFiles();
try{
if(login){
System.out.println("Your Are Logged In "+ftp.getStatus());
System.out.println("Working Directory is "+ftp.printWorkingDirectory());
System.out.println("Local Directory is "+localDIR);
System.out.println("Total Files Are "+files.length);
if(files != null && files.length >0 )
{
for(FTPFile fl:files)
{
if(!fl.isFile())
{
continue;
}
System.out.println(fl.getName());
OutputStream out;
out=new FileOutputStream(localDIR+"/"+fl.getName());
ftp.retrieveFile(fl.getName(), out);
out.close();
}
}
}
else
{
System.out.println("Sorry");
}
ftp.logout();
ftp.disconnect();
}
catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args) throws IOException {
FTPDownload ft=new FTPDownload();
ft.ftpDownload();
}
}

Related

I'm trying to lock file exclusively on read

I'm trying to lock a file exclusively on read.
I am using Java 7 on Windows 7.
The files are copied to a directory, and then I want to to read them correctly after copy. That's why I'm trying lock file exclusively on read.
But my program read file in the middle.
Can anyone please help me with this? Thanks!
package test;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
public class FileLockMove {
public static void main(String[] args) {
FileSystem fs = FileSystems.getDefault();
File dir = new File("c:/test_source/");
while(true){
for (File file : dir.listFiles()) {
try (FileChannel fc = (new RandomAccessFile(file, "rw")).getChannel()){
FileLock lock = fc.tryLock();
if(lock != null){
System.out.println("lock is not null\t" + file.getName());
System.out.println("addBody: " + file.getAbsolutePath() + "\t" + file.length());
}else{
System.out.println("lock is null\t" + file.getName());
continue;
}
} catch (Exception e) {
System.out.println("fail:" + file.getName()+"\t" + e.getMessage());
}
try {
Files.move(file.toPath(), fs.getPath("c:/test_dest/",file.getName()));
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Originally, the copy file is not Java, but I created a copy of my program slowly to confirm that I can lock exclusively.
package test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
public class FileCopySlow{
public static void main(String[] args) {
String source = "C:/test_orig/";
String dst ="C:/test_source/";
File file = new File(source);
File[] files = file.listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
if(name.matches(".*\\.bmp")){
return true;
}
return false;
}
});
for(File f: files){
copyStream(f, dst);
}
}
static void copyStream(File source,String dst){
try(BufferedInputStream br = new BufferedInputStream(new FileInputStream(source))){
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dst+source.getName()));
byte[] buff = new byte[100];
int len = -1;
while((len = br.read(buff)) >-1){
out.write(buff,0,len);
out.flush();
Thread.sleep(100);
}
out.close();
System.out.println(source.getName() + "\tfinish");
}catch(Exception e){
e.printStackTrace();
}
}
}

How to read file in ideone in java

I want to open, read, and edit file from my desktop. I am using Ideone online compiler. How do I read the file? I tried the following code:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
class demo
{
public static void main(String[] args)
{
System.out.println("Hello World!");
File file = new File("C:/Users/psanghavi/Desktop/admin_confirmation_original.txt");
if (!file.exists())
{
System.out.println("does not exist.");
return;
}
if (!(file.isFile() && file.canRead()))
{
System.out.println(file.getName() + " cannot be read from.");
return;
}
try
{
FileInputStream fis = new FileInputStream(file);
char current;
while (fis.available() > 0)
{
current = (char) fis.read();
System.out.print(current);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
My desktop has file named: admin_confirmation_original.txt
Currently, No. About the limit, Idebone FAQ say about this:
Can I write or read files in my program? - No
Can I access the network from my program? - No
You can learn more about many Ideone restricted rule at FAQ.
Ideoone doesn't support reading local files.
This is not an answer to your question, but wrt to the comments
if you want to read files hosted, you could access them using URL class.
import java.net.MalformedURLException;
import java.net.URL;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Demo {
public static void main(String[] args) throws IOException {
try {
final URL url = new URL("http://www.google.co.in/robots.txt");
//URL url = new URL("http://74.125.236.52/robots.txt");
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String str;
while (in.readLine() != null) {
str = in.readLine();
System.out.println(str);
}
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
I have not tried it on file hosting sites.There are a lot of free file hostings available just google it.

How to get absolute paths of files in a directory?

I have a directory with files, directories, subdirectories, etc. How I can get the list of absolute paths to all files and directories using the Apache Hadoop API?
Using HDFS API :
package org.myorg.hdfsdemo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HdfsDemo {
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/core-site.xml"));
conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/hdfs-site.xml"));
FileSystem fs = FileSystem.get(conf);
System.out.println("Enter the directory name :");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Path path = new Path(br.readLine());
displayDirectoryContents(fs, path);
}
private static void displayDirectoryContents(FileSystem fs, Path rootDir) {
// TODO Auto-generated method stub
try {
FileStatus[] status = fs.listStatus(rootDir);
for (FileStatus file : status) {
if (file.isDir()) {
System.out.println("This is a directory:" + file.getPath());
displayDirectoryContents(fs, file.getPath());
} else {
System.out.println("This is a file:" + file.getPath());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Writer a recursive function which takes a file and check if its a directory or not, if directory list out all files in it and in a for loop check if the file is a directory then recursively call or just return the list of files.
Something like this below but not exactly same (here I am returning only .java files)
private static List<File> recursiveDir(File file) {
if (!file.isDirectory()) {
// System.out.println("[" + file.getName() + "] is not a valid directory");
return null;
}
List<File> returnList = new ArrayList<File>();
File[] files = file.listFiles();
for (File f : files) {
if (!f.isDirectory()) {
if (f.getName().endsWith("java")) {
returnList.add(f);
}
} else {
returnList.addAll(recursiveDir(f));
}
}
return returnList;
}
with hdfs you can use hadoop fs -lsr .

How to unzip a zip folder containing different file formats using Java

I need to unzip a zipped directory containing different files' format like .txt, .xml, .xls etc.
I am able to unzip if the directory contains only .txt files but it fails with other files format. Below is the program that I am using and after a bit of googling, all I saw was similar approach -
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZipUtils {
public static void extractFile(InputStream inStream, OutputStream outStream) throws IOException {
byte[] buf = new byte[1024];
int l;
while ((l = inStream.read(buf)) >= 0) {
outStream.write(buf, 0, l);
}
inStream.close();
outStream.close();
}
public static void main(String[] args) {
Enumeration enumEntries;
ZipFile zip;
try {
zip = new ZipFile("myzip.zip");
enumEntries = zip.entries();
while (enumEntries.hasMoreElements()) {
ZipEntry zipentry = (ZipEntry) enumEntries.nextElement();
if (zipentry.isDirectory()) {
System.out.println("Name of Extract directory : " + zipentry.getName());
(new File(zipentry.getName())).mkdir();
continue;
}
System.out.println("Name of Extract fille : " + zipentry.getName());
extractFile(zip.getInputStream(zipentry), new FileOutputStream(zipentry.getName()));
}
zip.close();
} catch (IOException ioe) {
System.out.println("There is an IoException Occured :" + ioe);
ioe.printStackTrace();
}
}
}
Throws the below exception -
There is an IoException Occured :java.io.FileNotFoundException: myzip\abc.xml (The system cannot find the path specified)
java.io.FileNotFoundException: myzip\abc.xml (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
at updaterunresults.ZipUtils.main(ZipUtils.java:43)
When you try to open the file that is going to contain the extracted content, the error occurs.
This is because the myzip folder is not available.
So check if it indeed is not available and create it before extracting the zip:
File outputDirectory = new File("myzip");
if(!outputDirectory.exists()){
outputDirectory.mkdir();
}
As #Perception pointed out in the comments: The output location is relative to the active/working directory. This is probably not very convenient, so you might want to add the extraction location to the location of the extracted files:
File outputLocation = new File(outputDirectory, zipentry.getName());
extractFile(zip.getInputStream(zipentry), new FileOutputStream(outputLocation));
(of course you need also add outputLocation to the directory creation code)
This is a good example in which he showed to unzip all the formats (pdf, txt etc) have look its quite
or you can use this code might work (i haven't tried this)
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ZipUtils
{
private static final int BUFFER_SIZE = 4096;
private static void extractFile(ZipInputStream in, File outdir, String name) throws IOException
{
byte[] buffer = new byte[BUFFER_SIZE];
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(outdir,name)));
int count = -1;
while ((count = in.read(buffer)) != -1)
out.write(buffer, 0, count);
out.close();
}
private static void mkdirs(File outdir,String path)
{
File d = new File(outdir, path);
if( !d.exists() )
d.mkdirs();
}
private static String dirpart(String name)
{
int s = name.lastIndexOf( File.separatorChar );
return s == -1 ? null : name.substring( 0, s );
}
/***
* Extract zipfile to outdir with complete directory structure
* #param zipfile Input .zip file
* #param outdir Output directory
*/
public static void extract(File zipfile, File outdir)
{
try
{
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipfile));
ZipEntry entry;
String name, dir;
while ((entry = zin.getNextEntry()) != null)
{
name = entry.getName();
if( entry.isDirectory() )
{
mkdirs(outdir,name);
continue;
}
/* this part is necessary because file entry can come before
* directory entry where is file located
* i.e.:
* /foo/foo.txt
* /foo/
*/
dir = dirpart(name);
if( dir != null )
mkdirs(outdir,dir);
extractFile(zin, outdir, name);
}
zin.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Regards

How to check if file exist when downloading from FTP

I'm downloading from FTP server and I don't know exactly how to check if file already exist. What I want to do is that I retrieve filname from FTP server and then compare it with all files in folder. If file already exists then it compares next FTP filename with all files in folder and so on.
I already did comparison and it's working if all files from folder have same name as files on FTP server but if I add some older file then it downloads all files once again and I don't want that.
Here is my scratch code:
String[] names = client.listNames();
File folder = new File("c:\\test\\RTR_ZIP\\");
String[] filename = folder.list();
for (;i<names.length;i++) {
name = names[i];
exists=false;
if (name.contains(".zip")) {
if (filename.length == 0) {
new_file = new FileOutputStream("C:\\test\\RTR_ZIP\\" + name);
client.retrieveFile(name, new_file);
j++;
exists=true;
} else {
for (;k<filename.length;k++) {
name = names[i];
i++;
name1=filename[k];
// CHECK IF FILE EXISTS
if (!name.equals(name1)) {
new_file = new FileOutputStream("C:\\test\\RTR_ZIP\\" + name);
client.retrieveFile(name, new_file);
j++;
exists=true;
}
}
}//else
}//if contains .zip
}//for
Thanks in advance.
If your ftp server supports XCRC command it could be possible to compare checksum (CRC32) of local and remote file.
You could iterate all files in the folder and compare its crc with local one.
import java.io.File;
import java.io.IOException;
import java.net.SocketException;
import java.util.Scanner;
import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTPClient;
public class DownloadFile {
private FTPClient client = new FTPClient();
public void connect() throws SocketException, IOException {
client.connect("127.0.0.1");
client.login("user", "password");
}
public boolean hasXCRCSupport() throws IOException {
client.sendCommand("feat");
String response = client.getReplyString();
Scanner scanner = new Scanner(response);
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.contains("XCRC")) {
return true;
}
}
return false;
}
public boolean isSameFile() throws IOException {
if(hasXCRCSupport()) {
File file = new File("D:/test.txt");
String localCRC = Integer.toHexString((int) FileUtils.checksumCRC32(file)).toUpperCase();
client.sendCommand("XCRC /test.txt");
String response = client.getReplyString().trim();
System.out.println(response);
if(response.endsWith(localCRC)) {
return true;
}
}
return false;
}
public void logout() throws IOException {
client.logout();
}
public static void main(String[] args) throws SocketException, IOException {
DownloadFile downloadFile = new DownloadFile();
downloadFile.connect();
if(downloadFile.isSameFile()) {
System.out.println("remote file is same as local");
}
downloadFile.logout();
}
}
You should check for existence using java.io.File.exists and java.io.File.isFile()|isDirectory().
Maybe it will be useful to somebody with same problem. I made program by this method:
package javaapplication2;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.*;
public class DLFile {
public static void saveZIP() throws Exception {
FTPClient client = new FTPClient();
FileOutputStream new_file = null;
String server = "server";
String user = "user";
String pass = "pass";
String name = "";
String downloadFolder = "download_folder";
Boolean exists = null;
int i=0;
int j=0;
client.connect(server);
client.login(user,pass);
client.changeWorkingDirectory("/rtr/");
//read ftp content
String[] names = client.listNames();
File folder = new File(downloadFolder);
String[] filename = folder.list();
for (;i<names.length;i++) {
name = names[i];
exists=false;
if (name.contains(".zip")) {
if (filename.length == 0) {
new_file = new FileOutputStream(downloadFolder + name);
client.retrieveFile(name, new_file);
j++;
exists=true;
} else {
//CHECK IF FILE EXISTS
if (!new File(downloadFolder + name).exists()) {
new_file = new FileOutputStream(downloadFolder + name);
client.retrieveFile(name, new_file);
j++;
exists=true;
}
}//else
}//if contains .zip
}//for
if (exists = true) {
System.out.println("Downloading ZIP files: Downloaded " + j + " files");
} else System.out.println("Downloading ZIP files: Files already exist.");
client.logout();
}
}

Categories

Resources