我希望上传包含文件和一些json的多部分文件,并使用camel rest dsl将文件输出到本地文件夹。
我在路由中使用处理器使用HttpServletRequest.getPart()将多部分请求拆分为部分但是我收到以下错误:
java.lang.IllegalStateException: No multipart config for servlet
我已经向servlet添加了一个MultiPartFilter,并尝试使用和不使用multipartResolver bean。当我调试处理器时,我可以看到HttpServletRequest请求对象实际上是一个MultiPartFilter $ Wrapper但是异常仍在发生。
在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--
MultipartProcessor:
@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>
camel-context.xml中的多部分解析器:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
答案 0 :(得分:1)
检查请求是否为多部分,如果它是将您的配置添加到请求中:
if (request.getContentType() != null && request.getContentType().startsWith("multipart/form-data")) {
request.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, MULTI_PART_CONFIG);
}