Java ApacheCamel多部分路由
我正在尝试通过ApacheCamel将文件路由到HTTP文件上载API。但我有以下例外Java ApacheCamel多部分路由,java,file-upload,apache-camel,multipartform-data,Java,File Upload,Apache Camel,Multipartform Data,我正在尝试通过ApacheCamel将文件路由到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.Multipar
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映射的我的fileupload控制器方法需要一个multipartttpServletRequest
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());
}
}
是我为这个(Scala DSL)遵循的链接吗?当它说您需要一个InputStream时,消息是清楚的 方法build返回一个HttpEntity 您可以尝试使用getContent()方法 尝试更改您的:
exchange.getOut().setBody(entity.build());
致:
更新
发表评论后,您可以做的另一件事是:
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.build().writeTo(out);
InputStream inputStream = new ByteArrayInputStream(out.toByteArray());
exchange.getOut().setBody(inputStream);
谢谢你的回复。我尝试了你的建议,但得到了以下异常
java.lang.UnsupportedOperationException:多部分表单实体未实现#getContent()
还有其他方法吗?现在我可以将请求发送到API。但我认为它不包含实际的文件。请参阅下面的API实现public void handleFileUpload(multipartttpservletrequest){Iterator itrator=request.getFileNames();MultipartFile MultipartFile=request.getFile(itrator.next());}
在本例中,当请求在此处接收时,itrator
不提供要迭代的文件,因此我无法在服务器中获取文件。我能在这里做什么?请帮助我,我认为这是您构建MultipartEntityBuilder的方式。试试看,没有Camel也可以这样做:这里有一个关于如何使用它的示例:@Panchitoboy是的,我设法用HTTPClient在外部发布了请求。非常感谢你的帮助。。。
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.build().writeTo(out);
InputStream inputStream = new ByteArrayInputStream(out.toByteArray());
exchange.getOut().setBody(inputStream);