Converting file content to base64 with Apache Camel 2.10.2

Recently I wanted to convert input files of various formats into an XML message with certain meta information and the content of the input file as as base64-encoded element. Because I’m working on enterprise integration at the moment and we’re using Apache Camel a lot, I first tried to find an available solution from Camel.

Sure enough, if you’re using a version of Camel higher than 2.11.0, you can just use the shipped Base64DataFormat and be done. It’s as simple as this XML configuration:

<camelContext id="camel" xmlns="">
    <base64 id="base64" />
    <from uri="file://inputDirectory" />
    <marshal ref="base64" />
    <to uri="file://outputDirectory" />

Unfortunately, I’m stuck with Camel Version 2.10.2 - so no Base64DataFormat for me. I decided to roll my own DataFormat and use Apache Commons Base64.

My route (using the Spring DSL) now looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
  xmlns:xsi="" xmlns:camel=""

  <bean id="base64Dataformat" class="my.package.Base64DataFormat" />
  <camel:camelContext xmlns="">
          <from ref="file://inputDirectory" />
          <!-- convert input to base64 encoded string -->
              <custom ref="base64Dataformat" />
          <!-- convert into string, the velocity template isn't doing that -->
          <convertBodyTo type="java.lang.String" />
          <!-- wrap into mapping message schema -->
          <to uri="velocity:META-INF/templates/mappingmessage.vm" />
          <to uri="file://archive" />

The actual Base64 encoding looks quite like in Camel Version 2.11.x. Because I want to use a velocity template and wrap the base64 encoded string in XML, I have to convert the body into a java.lang.String - no idea why.

Now I can convert file content into base64 encoded string and pass them to other (mostly legacy) systems.

Weitere Artikel

Neue Tastatur: Pok3r Vortex RGB

New minecraft survival mod for 1.11.2

Änderungen beim Flug mit Quadkoptern

Ein paar Fotos

Nach den Crossfit Open

Crossfit Open WOD 17.5

Crossfit Open WOD 17.4

Crossfit Open WOD 17.3

Crossfit Open Workout 17.2

Crossfit Open Workout 17.1