Using RemoteFileTemplate
从 Spring Integration 3.0 开始,提供了 FtpSession`对象上的新抽象。该模板提供用于发送、检索(作为 `InputStream
)、移除和重命名文件的方法。此外,还提供了 execute`方法,允许调用方对会话执行多个操作。在所有情况下,模板负责可靠地关闭会话。有关更多信息,请参阅 Javadoc for `RemoteFileTemplate
。FTP 有一个子类:FtpRemoteFileTemplate
。
Starting with Spring Integration 3.0, a new abstraction is provided over the FtpSession
object.
The template provides methods to send, retrieve (as an InputStream
), remove, and rename files.
In addition, an execute
method is provided allowing the caller to execute multiple operations on the session.
In all cases, the template takes care of reliably closing the session.
For more information, see the
Javadoc for RemoteFileTemplate
.
There is a subclass for FTP: FtpRemoteFileTemplate
.
版本 4.1 添加了其他方法,包括 getClientInstance()
,它提供对底层 FTPClient
的访问,从而提供对低级 API 的访问。
Version 4.1 added additional methods, including getClientInstance()
, which provides access to the underlying FTPClient
and thus gives you access to low-level APIs.
并非所有 FTP 服务器都正确实现了 STAT <path>
命令。有些服务器会对一个不存在的路径返回一个正向结果。NLST
命令在路径是文件且存在时会可靠地返回名称。但是,由于路径是目录时 NLST
总会返回一个空列表,因此这无法支持检查一个空目录是否存在。由于该模板不知道路径是否表示一个目录,因此在路径看起来不存在时(使用 NLST
时),它必须执行额外的检查。这会增加开销,需要向服务器发出多次请求。自版本 4.1.9 起,FtpRemoteFileTemplate
提供了 FtpRemoteFileTemplate.ExistsMode
特性,它有以下选项:
Not all FTP servers properly implement the STAT <path>
command.
Some return a positive result for a non-existent path.
The NLST
command reliably returns the name when the path is a file, and it exists.
However, this does not support checking that an empty directory exists since NLST
always returns an empty list when the path is a directory.
Since the template does not know whether the path represents a directory, it has to perform additional checks when the path does not appear to exist (when using NLST
).
This adds overhead, requiring several requests to the server.
Starting with version 4.1.9, the FtpRemoteFileTemplate
provides the FtpRemoteFileTemplate.ExistsMode
property, which has the following options:
-
STAT
: Perform theSTAT
FTP command (FTPClient.getStatus(path)
) to check the path existence. This is the default and requires that your FTP server properly support theSTAT
command (with a path). -
NLST
: Perform theNLST
FTP command — `FTPClient.listName(path)`. Use this if you are testing for a path that is a full path to a file. It does not work for empty directories. -
NLST_AND_DIRS
: Perform theNLST
command first and, if it returns no files, fall back to a technique that temporarily switches the working directory by usingFTPClient.changeWorkingDirectory(path)
. SeeFtpSession.exists()
for more information.
由于我们知道 FileExistsMode.FAIL
情况始终仅仅查找一个文件(而不是一个目录),我们安全地对 FtpMessageHandler
和 FtpOutboundGateway
组件使用 NLST
模式。
Since we know that the FileExistsMode.FAIL
case is always only looking for a file (and not a directory), we safely use NLST
mode for the FtpMessageHandler
and FtpOutboundGateway
components.
对于任何其他情况,FtpRemoteFileTemplate
都可以被扩展,以便在重写后的 exist()
方法中实现自定义逻辑。
For any other cases, the FtpRemoteFileTemplate
can be extended to implement custom logic in the overridden exist()
method.
从 5.0 版本开始,可以使用新的 `RemoteFileOperations.invoke(OperationsCallback<F, T> action)`方法。该方法使多个 `RemoteFileOperations`调用可在相同线程限定 `Session`的范围内调用。当您需要将 `RemoteFileTemplate`的多项高级操作作为一项工作单元执行时,这非常有用。例如,`AbstractRemoteFileOutboundGateway`将其用于 `mput`命令实现,在其中我们对提供的目录中的每个文件执行 `put`操作,并对其子目录递归执行。有关更多信息,请参阅 Javadoc。
Starting with version 5.0, the new RemoteFileOperations.invoke(OperationsCallback<F, T> action)
method is available.
This method lets several RemoteFileOperations
calls be called in the scope of the same, thread-bounded, Session
.
This is useful when you need to perform several high-level operations of the RemoteFileTemplate
as one unit of work.
For example, AbstractRemoteFileOutboundGateway
uses it with the mput
command implementation, where we perform a put
operation for each file in the provided directory and recursively for its sub-directories.
See the Javadoc for more information.