在实际开发过程中,我们经常需要将某台服务器的文件自动上传到另外一台服务器。这时我们就需要一个文件自动上传工具,Apache Camel就很好的实现这个功能。
1.创建一个Spring Boot项目
选择依赖jar包:添加camel及web的依赖
确定创建项目信息
2.在POM文件中添加其他依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.4.3
com.liwei
camel-file
0.0.1-SNAPSHOT
camel-file
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.apache.camel
camel-spring-boot-starter
2.23.2
org.apache.httpcomponents
httpcore
4.4.9
org.apache.camel
camel-http4
2.23.2
org.apache.httpcomponents
httpmime
4.5.5
commons-io
commons-io
2.5
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
3.application.yml配置
file.from: file:E:/from/?recursive=true #源文件地址,recursive参数是递归from文件夹,具体参数见http://camel.apache.org/file2
file.to: E:/to/ #需要写入的目录
file.upload: http4://127.0.0.1:8080/upload #上传文件的接口
4.设置文件夹监听
设置文件夹监听,当文件夹有新增文件时,会自动触发上传。from
为需要上传的文件夹目录,本实例为file:E:/from/?recursive=true
,recursive
参数的意思是:查找子目录的文件,若为false
,则只上传当前目录的文件,如有其他要求,可查询http://camel.apache.org/file2
package com.liwei.camelfile;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class FileUploaderRoute extends RouteBuilder {
@Autowired
private CamelToolProcess camelToolProcess ;
@Override
public void configure() {
from("{{file.from}}")
.process(camelToolProcess )
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setHeader(Exchange.CONTENT_TYPE, constant("multipart/form-data"))
.to("{{file.upload}}");
}
}
5.文件处理CamelToolProcess
//构造HTTP请求文件上传格式
package com.liwei.camelfile;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.springframework.stereotype.Component;
import java.io.File;
import static org.apache.http.entity.ContentType.MULTIPART_FORM_DATA;
@Component
public class CamelToolProcess implements Processor {
@Override
public void process(Exchange exchange) {
MultipartEntityBuilder multipartEntityBuilder =
MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
String filename = (String)
exchange.getIn().getHeader(Exchange.FILE_NAME);
File file = exchange.getIn().getBody(File.class);
multipartEntityBuilder.addPart("file",
new FileBody(file, MULTIPART_FORM_DATA, filename));
exchange.getIn().setBody(multipartEntityBuilder.build());
}
}
6.上传文件接口
package com.liwei.camelfile.control;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@RestController
public class FileController {
@Value("${file.to}")
private String to;
@PostMapping("/upload")
@ResponseBody
public void upload(@RequestParam("file") MultipartFile file) throws IOException {
FileUtils.writeByteArrayToFile(new File(to + file.getOriginalFilename()), file.getBytes());
}
}
7.测试
上传完成后会自动创建一个.camel文件,将上传成功的文件写入.camel目录中
目标服务器的目录