I need to sync a file from a folder to a rest endpoint. So if a file is placed in a specific folder I need to send that file to a REST endpoint accepting multipart files. I am using apache camel to achieve this.
The REST endpoint is written in Spring and is as below:
@PostMapping("/")
public String handleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
storageService.store(file);
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded " + file.getOriginalFilename() + "!");
return "redirect:/";
}
I am new to Camel and have figured out how to poll the directory by building a route and fetch the file, but I am unable to figure out how this route has to be used to put this file to the rest endpoint. This is what I have tried:
@Component
public class SampleCamelRouter extends RouteBuilder {
@Override
public void configure() throws Exception {
from("file://target/input?delay=4000")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
File filetoUpload = exchange.getIn().getBody(File.class);
String fileName = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.addTextBody("fileName", fileName);
entity.addBinaryBody("file", filetoUpload);
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.build().writeTo(out);
InputStream inputStream = new ByteArrayInputStream(out.toByteArray());
exchange.getIn().setBody(inputStream);
}
})
.setHeader(Exchange.CONTENT_TYPE, constant("multipart/form-data"))
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.to("http://localhost:9090/");
}
}
But when I run this, I get the below exception in the rest server:
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
And on the camel side the request fails with a 500 error.
Any leads are appreciated.