我試圖通過Apache Camel將文件路由到HTTP文件上傳API。但我得到以下例外Apache駱駝多部分路由
org.apache.camel.InvalidPayloadException: No body available of type: java.io.InputStream but has value: [email protected] of type: org.apache.http.entity.mime.MultipartFormEntity on: [email protected] Caused by: No type converter available to convert from type: org.apache.http.entity.mime.MultipartFormEntity to the required type: java.io.InputStream with value [email protected] Exchange[[email protected]]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: org.apache.http.entity.mime.MultipartFormEntity to the required type: java.io.InputStream with value [email protected]]
任何人都可以在這裏幫助嗎? 以下是我試過到目前爲止
我的文件上傳控制方法與URL映射的API /文件上傳需要一個MultipartHttpServletRequest
MyCamelRouter.java
public class MyCamelRouter extends RouteBuilder {
@Override
public void configure() throws Exception {
from("file:C:/src")
.process(new MyProcessor())
.log("POST ${header.CamelFileName} to /upload")
.setHeader(Exchange.CONTENT_TYPE, constant("multipart/form-data"))
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.to("http:localhost:8080/sampleUploader/api/fileupload")
.log("HTTP response status: ${header.CamelHttpResponseCode}")
.log(LoggingLevel.DEBUG, "HTTP response body:\n${body}");
}
}
和MyProcessor.java
public class MyProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
File filetoUpload = exchange.getIn().getBody(File.class);
String fileName = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.addTextBody("fileName", fileName);
entity.addBinaryBody("file", new File(filePath));
exchange.getOut().setBody(entity.build());
}
}
This是我跟着鏈接,這個(斯卡拉DSL)
我的回答可以幫助您? – Panchitoboy 2015-03-06 10:56:49
@Panchitoboy是的我設法用HTTPClient在外部發布請求。非常感謝您的幫助... – 2015-03-09 18:03:09