【发布时间】:2015-03-03 21:23:47
【问题描述】:
我正在尝试通过 Apache Camel 将文件路由到 HTTP 文件上传 API。但是我遇到了以下异常
org.apache.camel.InvalidPayloadException: No body available of type: java.io.InputStream but has value: org.apache.http.entity.mime.MultipartFormEntity@71ef4d4f of type: org.apache.http.entity.mime.MultipartFormEntity on: org.apache.camel.component.file.GenericFileMessage@7e693963. 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 org.apache.http.entity.mime.MultipartFormEntity@71ef4d4f. Exchange[org.apache.camel.component.file.GenericFileMessage@7e693963]. 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 org.apache.http.entity.mime.MultipartFormEntity@71ef4d4f]
有人可以帮忙吗? 以下是我到目前为止尝试过的
与 URL api/fileupload 映射的文件上传控制器方法需要 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 是我为此关注的链接(Scala DSL)
【问题讨论】:
-
@Panchitoboy 是的,我设法使用 HTTPClient 在外部发布请求。非常感谢您的帮助...
标签: java file-upload apache-camel multipartform-data