我试图编写一个程序来使用Apache骆驼执行基于内容的路由。我想轮询一个特定的文件夹,然后根据文件类型将文件分成不同的文件夹。我写了以下代码:-
public void configure() throws Exception {
Scanner sc = new Scanner(System.in);
String path = sc.next();
from("file:"+path+"?noop=true")
.to("log:?level=INFO&showBody=true&showHeaders=true")
.choice()
.when(header("CamelFileNameConsumed").endsWith(".html"))
.to("file:HTML files")
.when(header("CamelFileNameConsumed").endsWith(".txt"))
.to("file:Text Files")
.when(header("CamelFileNameConsumed").endsWith(".cpp"))
.to("file:Cpp Files")
.when(header("CamelFileNameConsumed").endsWith(".xlsx"))
.to("file:Excel Files")
.when(header("CamelFileNameConsumed").endsWith(".png"))
.to("file:Images")
.when(header("CamelFileNameConsumed").endsWith(".jpg"))
.to("file:Images")
.otherwise()
.to("file:other")
.end()
.to("file:all");
}
在编写时,我意识到图像文件和视频文件可能具有多种格式。.我想避免为每种文件类型编写新的代码行。例如:我想在一个.when()条件中包括所有图像文件类型,例如jpeg,png等。有没有办法做到这一点,因为||(或)在这种情况下似乎不起作用。如果有人也可以提出对此代码的任何改进,我将感到很高兴。