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

查看:30
本文介绍了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 请求.使用这个,将准备附件,到 交换.

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

之后你需要以某种方式转换 AttachmentInputStream 并填充 CamelFileName 标题.有了这个任务可以帮助你小型Processor.

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天全站免登陆