LoginSignup
7
7

More than 5 years have passed since last update.

Apache Camelでファイルをアップロードする

Posted at

ファイル一覧を出力したあとに自作のWebアプリにアップロードするまでのメモ。

pom.xmlにcamel-http4とhttpmimeを追加。

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.0</modelVersion>
    <groupId>net.camel.client</groupId>
    <artifactId>camel-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <camel.version>2.13.2</camel.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>${camel.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>17.0</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-http4</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.3.2</version>
        </dependency>
    </dependencies>
</project>

RouteBuilderは別のクラスに切り出し。

MyRouteBuilder.java
package net.camel.client.route;

import net.camel.client.processor.FileListProcessor;
import net.camel.client.processor.FileListToTsvStringProcessor;
import net.camel.client.processor.MultipartEntityProcessor;

import org.apache.camel.builder.RouteBuilder;

public class MyRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        // 指定ディレクトリ内のファイルの一覧を出力
        from("direct:report")
                .process(new FileListProcessor())
                .process(new FileListToTsvStringProcessor())
                .to("file:reports/?fileName=report.tsv");

        // ファイルをアップロード
        from("file:reports/?fileName=report.tsv")
                .process(new MultipartEntityProcessor())
                .to("http4:localhost:8080/upload");
    }
}

camel-http4ではExchangeのbodyにファイルが格納されている場合に自動でアップロード用のリクエストが作られるらしいけどイマイチうまくいかなかったのでMultipartEntityを作るProcessorを自作。

MultipartEntityProcessor.java
package net.camel.client.processor;

import java.io.File;

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;

public class MultipartEntityProcessor implements Processor {

    @Override
    public void process(Exchange exchange) throws Exception {
        File file = exchange.getIn().getBody(File.class);
        FileBody fileBody = new FileBody(file);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addPart("file", fileBody);
        exchange.getIn().setBody(builder.build());
    }

}
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7