【问题标题】:Spring Camel Rest route problem with file upload文件上传的Spring Camel Rest路由问题
【发布时间】:2020-11-02 13:17:17
【问题描述】:

过去 2 天我一直在努力解决以下问题。我正在尝试通过 POST 路由上传 csv 文件。该文件是从 html 表单中选择上传的。

<form method="POST" action="uploadFile" enctype="multipart/form-data">
    <div>
        <div style="display:inline;">
            <label path="file">Select a file to upload</label>
        </div>
        <div style="display:inline;">
            <input type="file" name="file" />
        </div>
    </div>
    <div>
        <input type="submit" value="Submit" />
    </div>
</form>

这是骆驼 POST 路线:

rest(properties.getRestEndpointPrefix() + properties.getRestEndpointUploadFileUrl())
    .post()
    .consumes(MediaType.MULTIPART_FORM_DATA_VALUE)
    .route()
    .routeId("postUploadForm")
    .process((exchange) -> {
        InputStream is = exchange.getIn().getBody(InputStream.class);
        Map<String, String> body = exchange.getIn().getBody(Map.class);
        MimeBodyPart mimeMessage = new MimeBodyPart(is);
        DataHandler dh = mimeMessage.getDataHandler();
        exchange.getIn().setBody(dh.getInputStream());
        exchange.getIn().setHeader(Exchange.FILE_NAME, dh.getName());
    })
    .to(properties.getIncomingUri());

真正对我有用的唯一解决方案是来自 post 的 Bedla,使用 MimeBodyPart。出于某种原因,每当我尝试使用 spring 的 MultiPart 和 children 时,当接触到骆驼体时它总是为空,我认为这是因为我不/不能使用 @RequestPart 注释。

我的问题是现有解析在文件末尾保留了多部分/表单数据边界,从而破坏了随后发生的任何 csv 解析。下面是边界,由邮递员生成。

----------------521923768224522097053873--

任何帮助表示赞赏,提前非常感谢!

[编辑]

使用@karine 的解决方案,我确实得到了干净的身体并事先保留了文件名。下面是最后的休息路线:

rest(properties.getRestEndpointPrefix() + properties.getRestEndpointUploadFileUrl())
    .post()
    .consumes(MediaType.MULTIPART_FORM_DATA_VALUE)
    .route()
    .routeId("postUploadForm")
    .process((exchange) -> {
        //Keep the file name from the unmarshalled file. After unmarshalling, fileName will be lost
        MimeBodyPart mimeMessage = new MimeBodyPart(exchange.getIn().getBody(InputStream.class));
        exchange.getIn().setHeader(Exchange.FILE_NAME, mimeMessage.getDataHandler().getName());
    })
    .unmarshal()
    .mimeMultipart()
    .to(properties.getIncomingUri());

【问题讨论】:

    标签: spring spring-boot file-upload multipartform-data spring-camel


    【解决方案1】:

    这是我检索上传文件内容的方式

        ...
        .post()
         .bindingMode(RestBindingMode.off)
         .route()
            .routeId("postUploadForm")
            .unmarshal().mimeMultipart()
            .process((exchange) -> {
                InputStream is = exchange.getIn().getBody(InputStream.class);
                // is contains the content file
                ...
            })
    

    但是通过这种方式,我无法获取文件名。 希望对大家有帮助

    【讨论】:

    • 因此,在尝试之后,通过解组,您会丢失文件名,正如您所说,但我已经在处理器中获取了该文件名。我将编辑我的问题以显示最终代码。非常感谢您的指导!!
    【解决方案2】:

    您可以尝试将您的 .process 方法替换为以下方法!

        .process(exchange -> {
            String strMessage = FileUtils.readFileToString(
                    new File(yourFileName), StandardCharsets.UTF_8);
            exchange.getMessage().setBody(strMessage);
            exchange.getMessage().setHeader(Exchange.FILE_NAME, yourFileName);
        })
    

    【讨论】:

    • 感谢您的回复!不幸的是,它没有用。它以文件名的形式读取文件数据并抛出 IOException。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2017-09-21
    • 2021-03-17
    相关资源
    最近更新 更多