Camel Rest DSL检索HTTP POST多部分文件 [英] Camel Rest DSL retrieve HTTP POST multipart File

查看:165
本文介绍了Camel Rest DSL检索HTTP POST多部分文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的路由器类如下所示,我正在尝试上传视频文件并将其存储到文件位置。

My Router class looks like below and i am trying to upload a video file and store it to a File location.

SpringBootRouter.java

package com.camelrest;

import java.util.HashMap;
import java.util.Map;

import org.apache.camel.component.restlet.RestletComponent;
import org.apache.camel.spring.boot.FatJarRouter;
import org.restlet.Component;
import org.restlet.ext.spring.SpringServerServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class MySpringBootRouter extends FatJarRouter {

    @Autowired
    private MultipartProcessor multipartProcessor;

    @Override
    public void configure() {

        restConfiguration().component("restlet");

        rest("/upload").post().to("direct:upload");

        from("direct:upload")
        .to("file://E:/RestTest");

    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {

        SpringServerServlet serverServlet = new SpringServerServlet();
        ServletRegistrationBean regBean = new ServletRegistrationBean(
                serverServlet, "/rest/*");

        Map<String, String> params = new HashMap<String, String>();

        params.put("org.restlet.component", "restletComponent");

        regBean.setInitParameters(params);

        return regBean;
    }

    @Bean
    public Component restletComponent() {
        return new Component();
    }

    @Bean
    public RestletComponent restletComponentService() {
        return new RestletComponent(restletComponent());
    }    
}

我正在尝试使用邮递员上传视频文件每个以下屏幕截图:

I am trying to upload a video file using postman as per below screenshot :

我的内容我上传的文件保存的文件名带有骆驼生成的一些随机骆驼ID

My contents of the file that i upload are saved with a file name with some random camel ID generated by camel

但是我希望在正文中传递文件名

However i want the filename that is passed in body


SampleVideo_1280x720_10mb.mp4

SampleVideo_1280x720_10mb.mp4

作为文件名并从正文中删除以下内容

to be the name of the file and remove the following contents from the body

----------------------------948281627232093197119960
Content-Disposition: form-data; name="file"; filename="SampleVideo_1280x720_10mb.mp4"
Content-Type: video/mp4

所以最终输出可以是使用邮递员上传过程中使用的文件名上传的视频

So final output can be the video uploaded with the filename used during the upload with postman

推荐答案

您可以使用 MimeMultipartDataFormat 取消封送Multipart请求。使用此工具,将准备附件,以 Exchange

You can use MimeMultipartDataFormat to unmarshal Multipart request. Using this, will prepare attachments, to Exchange.

之后,您需要以某种方式转换 附件 InputStream 并填充 CamelFileName 标头。通过此任务可以帮助您小型的处理器

After that you need somehow convert Attachment to InputStream and fill CamelFileName header. With this task can help you small Processor.

路线:

from("direct:upload")
        .unmarshal().mimeMultipart().split().attachments()
        .process(new PrepareFileFromAttachment())
        .to("file://C:/RestTest");

处理器:

class PrepareFileFromAttachment implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception {
        DataHandler dataHandler = exchange.getIn().getBody(Attachment.class).getDataHandler();
        exchange.getIn().setHeader(Exchange.FILE_NAME, dataHandler.getName());
        exchange.getIn().setBody(dataHandler.getInputStream());
    }
}






如果您的表单仅包含一个表单输入,则上述方法不起作用。这是因为 MimeMultipartDataFormat 将第一形式的输入编组到正文中(不存储文件名),并将其他形式的输入存储到存储文件名的附件中。
在这种情况下,您需要创建 Processor 直接读取 InputStream

路线:

from("direct:upload")
        .process(new ProcessMultipartRequest())
        .to("file:c://RestTest");

处理器

public class ProcessMultipartRequest implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception {
        InputStream is = exchange.getIn().getBody(InputStream.class);
        MimeBodyPart mimeMessage = new MimeBodyPart(is);
        DataHandler dh = mimeMessage.getDataHandler();
        exchange.getIn().setBody(dh.getInputStream());
        exchange.getIn().setHeader(Exchange.FILE_NAME, dh.getName());
    }
}

这篇关于Camel Rest DSL检索HTTP POST多部分文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆