简体   繁体   中英

Java and FTP server

I have a problem with connection with FTP server. I have a application, which must read data from files. I have code where I searching files at my local disk, but I must change that because I have all data at FTP server. At this time I using:

FileChannel fc = new FileInputStream("C:/Data/" + nameFile)
                    .getChannel();

where nameFile is name my file. I create channel where I load data from file from local disk. Can I change that code that I can search files at FTP server?

I don't fully understand your post, but it sounds like you are looking for some code to check whether a file exists on the remote FTP server, correct? If so, then you will want to do the following:

  1. Connect to server and authenticate.

  2. Navigate to directory on remote system

  3. Perform a directory listing of remote system

  4. Check to see if any of the files in directory listing match the file you are looing for.

I've done this successfully using Secure FTP Factory at http://www.jscape.com/products/components/java/secure-ftp-factory/

Example Code

Ftp ftp = new Ftp(hostname,username,pass);
ftp.connect();

// get directory listing

Enumeration listing = ftp.getDirListing();

// enumerate thru listing

while(listing.hasMoreElements()) {

FtpFile file = (FtpFile)listing.nextElement();

// check to see if filename matches 
System.out.println("Filename: " + file.getFilename());

}

You have to use the FTP protocol's commands and preferably an FTP library. Apache Commons' FTPClient is quite good.

Why not just use something like ftp4j? http://www.sauronsoftware.it/projects/ftp4j/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM