0

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.

rd22
  • 1,032
  • 1
  • 18
  • 34

1 Answers1

-1

use headers = "Content-Type= multipart/form-data", into controller.

The problem isn't in your code - it's in your request. You're missing boundary in your multipart request. As it said in specification here:

The Content-Type field for multipart entities requires one parameter, "boundary", which is used to specify the encapsulation boundary. The encapsulation boundary is defined as a line consisting entirely of two hyphen characters ("-", decimal code 45) followed by the boundary parameter value from the Content-Type header field. This and this posts should also be helpful.

Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17
  • Hi, Thanks for the comment but I have already checked it at https://stackoverflow.com/questions/17462642/spring-rest-the-request-was-rejected-because-no-multipart-boundary-was-found. And it did not help me. – rd22 Jul 24 '17 at 07:18