Akshay Sakunde
Akshay Sakunde

Reputation: 55

How to Upload txt file using apache camel rest end point from HTML page

I am new to apache camel. I am trying to upload txt file from HTML page but not getting file in correct form at receiver end. I am getting below mentioned data in exchange body, getting webform boundary along with the content of the file.

------WebKitFormBoundaryOAiLMJtrA2g4CB32
Content-Disposition: form-data; name="test.txt"

Hello how are you..?
------WebKitFormBoundaryOAiLMJtrA2g4CB32
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain


------WebKitFormBoundaryOAiLMJtrA2g4CB32--  

I am using rest end point of apache camel.

HTML Page:

<form method="post" action="uploadFile" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="Upload" />
</form>

Apache Rest Endpoint:

rest("/uploadFile").post("/")
    .param("file").endParam()
    .to("bean:BeanName?method=converterMethod(Exchange)"); 

How could I extract the file content from this? I need to extract only content like -> Hello how are you..?

Upvotes: 1

Views: 1169

Answers (1)

erayerdem
erayerdem

Reputation: 836

here an example about how to read multipart/formdata from apache camel . it reads from http and persist to filesystem as newfile.txt

rest("send")
            .post("data")
            .consumes("multipart/form-data")
            .to("direct:c");

    from("direct:c")
            .setHeader("CamelFileName", constant("newfile.txt"))
            .process(e -> {
                Map<String, DataHandler> attachments = e.getIn(AttachmentMessage.class).getAttachments();
                byte[] bytes = attachments.get("hello1.txt").getInputStream().readAllBytes();
                e.getIn().setBody(bytes);
            }).to("file:mydirect")
            .setBody(constant("Successfull Job"));

Curl request is

curl --location --request POST 'localhost:8080/send/data' \--form 'hellworld=@"/Users/erayerdem/Desktop/hello1.txt"'

Application Response

Successfull Job

created file location

javaAppDirector/mydirect/newfile.txt

Upvotes: 1

Related Questions