Java 使用camel rest DSL上载多部分文件

Java 使用camel rest DSL上载多部分文件,java,rest,file-upload,apache-camel,multipartform-data,Java,Rest,File Upload,Apache Camel,Multipartform Data,我希望上传一个包含一个文件和一些json的多部分文件,并使用camel rest dsl将该文件输出到本地文件夹 我正在使用路由中的处理器使用HttpServletRequest.getPart()将多部分请求拆分为多个部分,但我收到以下错误: java.lang.IllegalStateException: No multipart config for servlet 我已经向servlet添加了一个MultiPartFilter,并尝试了使用和不使用multipartresolverbe

我希望上传一个包含一个文件和一些json的多部分文件,并使用camel rest dsl将该文件输出到本地文件夹

我正在使用路由中的处理器使用HttpServletRequest.getPart()将多部分请求拆分为多个部分,但我收到以下错误:

java.lang.IllegalStateException: No multipart config for servlet
我已经向servlet添加了一个MultiPartFilter,并尝试了使用和不使用multipartresolverbean。在调试处理器时,我可以看到HttpServletRequest请求对象实际上是一个MultiPartFilter$包装器,但异常仍在发生

在html页面中上载表单:

<html>
<body>
    <form action="http://localhost:8080/content/publish" method="post" enctype="multipart/form-data">
        <p>
            Json: <input type="text" name="json"/>
        </p>
        <p>
            File: <input type="file" name="uploadedFile" multiple="true" />
        </p>
        <input type="submit" value="Upload" />
    </form>
</body>
</html>
请求:

rest("/content").post("/publish").consumes("multipart/form-data").route().process(new MultipartProcessor()).to("file:src/main/resources/testUpload");
------WebKitFormBoundaryOAiLMJtrA2g4CB32
Content-Disposition: form-data; name="json"

{ "json" : "test" }
------WebKitFormBoundaryOAiLMJtrA2g4CB32
Content-Disposition: form-data; name="uploadedFile"; filename="test.txt"
Content-Type: text/plain


------WebKitFormBoundaryOAiLMJtrA2g4CB32--
if (request.getContentType() != null && request.getContentType().startsWith("multipart/form-data")) {
  request.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, MULTI_PART_CONFIG);
}
多部件处理器:

@Override
public void process(Exchange exchange) throws Exception {
    HttpMessage message = exchange.getIn(HttpMessage.class);
    HttpServletRequest request = message.getRequest();
    Part file = request.getPart("uploadedFile");
    // then process to output file
}
在web.xml中:

  <servlet>
    <display-name>Camel Http Transport Servlet</display-name>
    <servlet-name>CamelServlet</servlet-name>
    <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>CamelServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

  <filter>
        <filter-name>MultipartFilter</filter-name>
        <filter-class>org.eclipse.jetty.servlets.MultiPartFilter</filter-class>
   </filter>  

   <filter-mapping>
        <filter-name>MultipartFilter</filter-name>
        <servlet-name>CamelServlet</servlet-name>  
   </filter-mapping>

驼峰Http传输Servlet
骆驼队
org.apache.camel.component.servlet.CamelHttpTransportServlet
1.
骆驼队
/*
多部件滤波器
org.eclipse.jetty.servlets.MultiPartFilter
多部件滤波器
骆驼队
camel-context.xml中的多部分解析器:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

检查请求是否为多部分,如果为多部分,请将您的配置添加到请求中:

rest("/content").post("/publish").consumes("multipart/form-data").route().process(new MultipartProcessor()).to("file:src/main/resources/testUpload");
------WebKitFormBoundaryOAiLMJtrA2g4CB32
Content-Disposition: form-data; name="json"

{ "json" : "test" }
------WebKitFormBoundaryOAiLMJtrA2g4CB32
Content-Disposition: form-data; name="uploadedFile"; filename="test.txt"
Content-Type: text/plain


------WebKitFormBoundaryOAiLMJtrA2g4CB32--
if (request.getContentType() != null && request.getContentType().startsWith("multipart/form-data")) {
  request.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, MULTI_PART_CONFIG);
}

来源:

谢谢,这已经修复了我最初的IllegalStateException问题,但现在我收到了一个IOException:缺少多部分请求的内容。。在请求到达处理器之前猜测有什么东西正在读取请求,但我不确定是什么。@Sian,我还面临着同样的问题“IOException:多部分请求缺少内容”。你找到什么解决方案了吗?对我来说是exchange.getIn(HttpMessage.class);返回空Always相关问题: