Skip to content

InspireTechnologies/ftp-fs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ftp-fs

The ftp-fs library provides support for FTP and FTPS NIO.2 file systems, allowing FTP servers to be accessed in a similar way to local file systems.

Creating file systems

If the FTP file system library is available on the class path, it will register FileSystemProviders for schemes ftp and ftps. This allows you to create FTP and FTPS file systems using the newFileSystem methods of class FileSystems. You can use classes FTPEnvironment and FTPSEnvironment to help create the environment maps for those methods:

FTPEnvironment env = new FTPEnvironment()
        .withCredentials(username, password);
FileSystem fs = FileSystems.newFileSystem(URI.create("ftp://example.org"), env);

Note that, for security reasons, it's not allowed to pass the credentials as part of the URI when creating a file system. It must be passed through the environment, as shown above.

Creating paths

After a file system has been created, Paths can be created through the file system itself using its getPath method. As long as the file system is not closed, it's also possible to use Paths.get. Note that if the file system was created with credentials, the username must be part of the URL. For instance:

// created without credentials
Path path1 = Paths.get(URI.create("ftp://example.org"));
// created with credentials
Path path2 = Paths.get(URI.create("ftp://username@example.org"));

If the username in the URI does not match the username used to create the file system, this will cause a FileSystemNotFoundException to be thrown.

Attributes

File attributes

FTP file systems fully support read-access to the following attributes:

Attempting to set any of these attributes, either through one of the file attribute views or through a file system, will result in an UnsupportedOperationException.

File store attributes

When calling getAttribute on a file store, the following attributes are supported:

Because FTP servers do not return these values, these methods will all return Long.MAX_VALUE.

There is no support for FileStoreAttributeView. Calling getFileStoreAttributeView on a file store will simply return null.

FTPS support

To create an FTPS connection instead of an FTP connection, use ftps as the scheme. Also, use class FTPSEnvironment instead of class FTPEnvironment to create the file system. Using an FTPEnvironment instance is still allowed, but you will not be able to specify FTPS specific properties.

Error handling

Unfortunately, FTP servers can use the same code for multiple erroneous situations. For example, code 550 can indicate that a file does not exist, or that access to an existing file is not allowed. Because of this, most methods do not throw the correct exception (NoSuchFileException, AccessDeniedException, etc).

To allow this behaviour to be modified, class FTPEnvironment has method withFileSystemExceptionFactory that allows you to specify a custom FileSystemExceptionFactory implementation which will be used to create exceptions based on the reply code and string of the command that triggered the error. By default, an instance of class DefaultFileSystemExceptionFactory is used.

The ftp-fs library provides subclasses for FileSystemException and several of its subclasses to allow the FTP server's reply code and string to be reserved. Instances of these classes can be returned by FileSystemExceptionFactory implementations as needed.

Thread safety

The FTP protocol is fundamentally not thread safe. To overcome this limitation, FTP file systems maintain multiple connections to FTP servers. The number of connections determines the number of concurrent operations that can be executed. If all connections are busy, a new operation will block until a connection becomes available. Class FTPEnvironment has method withClientConnectionCount that allows you to specify the number of connections to use. If no connection count is explicitly set, the default will be 5. It also has method withClientConnectionWaitTimeout that can be used to control how long to wait before a connection is available. The default is 0 which means wait indefinitely.

When a stream or channel is opened for reading or writing, the connection will block because it will wait for the download or upload to finish. This will not occur until the stream or channel is closed. It is therefore advised to close streams and channels as soon as possible.

Connection management

Because FTP file systems use multiple connections to an FTP server, it's possible that one or more of these connections become stale. Class FTPFileSystemProvider has static method keepAlive that, if given an instance of an FTP file system, will send a keep-alive signal (NOOP) over each of its idle connections. You should ensure that this method is called on a regular interval.

Limitations

FTP file systems knows the following limitations:

  • All paths use / as separator. / is not allowed inside file or directory names.
  • File attributes cannot be set, either when creating files or directories or afterwards.
  • Symbolic links can be read and traversed, but not created.
  • There is no support for hard links.
  • Files can be marked as executable if the FTP server indicates it is. That does not mean the file can be executed in the local JVM.
  • SeekableByteChannel is supported because it's used by Files.createFile. However, these channels do not support seeking specific positions or truncating.
  • There is no support for UserPrincipalLookupService.
  • There is no support for WatchService.