Spring BOOT + Apache Camel实现文件自动上传同步

在实际开发过程中,我们经常需要将某台服务器的文件自动上传到另外一台服务器。这时我们就需要一个文件自动上传工具,Apache Camel就很好的实现这个功能。

1.创建一个Spring Boot项目

进入https://start.spring.io/,创建Spring Boot项目,添加camelweb的依赖。本实例使用的是http上传文件,也可以使用其他上传文件形式,如:FTP等。
Spring BOOT + Apache Camel实现文件自动上传同步_第1张图片

2.在POM文件中添加其他依赖

在pom文件中添加commons-io等依赖,具体pom.xml文件如下


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0modelVersion>
	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>2.1.4.RELEASEversion>
		<relativePath/> 
	parent>
	<groupId>com.pengchaogroupId>
	<artifactId>camel-toolsartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<name>camel-toolsname>
	<description>Demo project for Spring Bootdescription>

	<properties>
		<java.version>1.8java.version>
	properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>
		<dependency>
			<groupId>org.apache.camelgroupId>
			<artifactId>camel-spring-boot-starterartifactId>
			<version>2.23.2version>
		dependency>

		<dependency>
			<groupId>org.apache.httpcomponentsgroupId>
			<artifactId>httpcoreartifactId>
			<version>4.4.9version>
		dependency>

		<dependency>
			<groupId>org.apache.camelgroupId>
			<artifactId>camel-http4artifactId>
			<version>2.23.2version>
		dependency>

		<dependency>
			<groupId>org.apache.httpcomponentsgroupId>
			<artifactId>httpmimeartifactId>
			<version>4.5.5version>
		dependency>

		<dependency>
			<groupId>commons-iogroupId>
			<artifactId>commons-ioartifactId>
			<version>2.5version>
		dependency>

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>
	dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>
		plugins>
	build>

project>

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=truerecursive参数的意思是:查找子目录的文件,若为false,则只上传当前目录的文件,如有其他要求,可查询http://camel.apache.org/file2

package com.pengchao.cameltools.listener;

import com.pengchao.cameltools.process.CamelToolProcess;
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.pengchao.cameltools.process;

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.pengchao.cameltools.controller;

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.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")
    public void upload(MultipartFile file) throws IOException {
        FileUtils.writeByteArrayToFile(new File(to + file.getOriginalFilename()), file.getBytes());
    }
}

7.测试

上传完成后会自动创建一个.camel文件,将上传成功的文件写入.camel目录中
Spring BOOT + Apache Camel实现文件自动上传同步_第2张图片
Spring BOOT + Apache Camel实现文件自动上传同步_第3张图片
服务器的目录
Spring BOOT + Apache Camel实现文件自动上传同步_第4张图片

你可能感兴趣的:(spring,spring,boot,camel)