24.21. 使用固定名称消耗单个文件
当您要下载单个文件并知道文件名时,您可以使用 fileName=myFileName.txt
告知 Camel 要下载的文件的名称。默认情况下,消费者仍将 FTP LIST 命令执行目录列表,然后根据 fileName
选项过滤这些文件。虽然在这个用例中,可能需要通过设置 useList=false
来关闭目录列表。例如,用于登录到 FTP 服务器的用户帐户可能没有执行 FTP LIST 命令的权限。因此,您可以使用 useList=false
关闭它,然后提供文件的固定名称来下载 fileName=myFileName.txt
,然后 FTP 使用者仍然可以下载该文件。如果出于某种原因的文件不存在,则 Camel 默认会抛出异常,您可以通过设置 ignoreFileNotFoundOrPermissionError=true
来关闭此异常并忽略它。
例如,要有一个 Camel 路由选择单个文件,并在使用后将其删除
from("ftp://admin@localhost:21/nolist/?password=admin&stepwise=false&useList=false&ignoreFileNotFoundOrPermissionError=true&fileName=report.txt&delete=true") .to("activemq:queue:report");
from("ftp://admin@localhost:21/nolist/?password=admin&stepwise=false&useList=false&ignoreFileNotFoundOrPermissionError=true&fileName=report.txt&delete=true")
.to("activemq:queue:report");
请注意,我们使用了以上介绍的所有选项。
您还可以在 ConsumerTemplate
中使用它。例如,要下载单个文件(如果存在),并将文件内容作为 String 类型获取:
String data = template.retrieveBodyNoWait("ftp://admin@localhost:21/nolist/?password=admin&stepwise=false&useList=false&ignoreFileNotFoundOrPermissionError=true&fileName=report.txt&delete=true", String.class);
String data = template.retrieveBodyNoWait("ftp://admin@localhost:21/nolist/?password=admin&stepwise=false&useList=false&ignoreFileNotFoundOrPermissionError=true&fileName=report.txt&delete=true", String.class);