24.21. 使用固定名称消耗单个文件
如果要下载单个文件并知道文件名,您可以使用 fileName=myFileName.txt
告知 Camel 要下载的文件名。默认情况下,使用者仍将执行 FTP LIST 命令来执行目录列表,然后根据 fileName
选项过滤这些文件。尽管在这个用例中,最好通过设置 List=false
来关闭目录列表。例如,用于登录 FTP 服务器的用户帐户可能不具有 FTP LIST 命令的权限。因此,您可以使用List=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);