【问题标题】:How to Upload txt file using apache camel rest end point from HTML page如何从 HTML 页面使用 apache 骆驼休息端点上传 txt 文件
【发布时间】:2021-12-01 02:04:33
【问题描述】:

我是 apache 骆驼的新手。我正在尝试从 HTML 页面上传 txt 文件,但没有在接收端以正确的形式获取文件。我在交换正文中获得了以下提到的数据,获取了网络表单边界以及文件的内容。

------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--  

我正在使用 apache camel 的休息端点。

HTML 页面:

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

Apache 休息端点:

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

如何从中提取文件内容? 我只需要提取像 -> Hello how are you..?

这样的内容

【问题讨论】:

    标签: java spring file-upload apache-camel multipartform-data


    【解决方案1】:

    这里有一个关于如何从 apache camel 读取 multipart/formdata 的示例。它从 http 读取并作为 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 --location --request POST 'localhost:8080/send/data' \--form 'hellworld=@"/Users/erayerdem/Desktop/hello1.txt"'
    

    应用程序响应

    Successfull Job
    

    创建的文件位置

    javaAppDirector/mydirect/newfile.txt

    【讨论】:

    • 什么是 'AttachmentMessage.class' 属于哪个库?我收到这个类的错误。
    • txt 文件可以是任何 txt 文件。在处理上面的代码时,我在进程中得到了“null”。
    • Camel Core 包括 AttachmentMessage 类和结帐附件地图大小。我在我的电脑上试过这段代码。它工作正常
    • 'AttachmentMessage' 是一个接口,所以我使用 Message 类,但附件映射大小为 0。我正在通过 HTML 页面上传。
    • 还有没有其他配置?
    猜你喜欢
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 2019-07-07
    相关资源
    最近更新 更多