Apache Camel multipart route

旧街凉风 提交于 2019-12-11 10:36:51

问题


I'm trying to route a file to HTTP file upload API via Apache Camel. But I'm getting following exception

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]

Can anyone help here? Following is what I tried so far

My fileupload controller method mapped with the URL api/fileupload expects a 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}");
}

}

and in 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 is the link I followed for this(Scala DSL)


回答1:


The message is clear when it say that you need an InputStream

The method build returns an HttpEntity.

http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntityBuilder.html

You can try with the method getContent()

http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpEntity.html?is-external=true

Try to change your:

exchange.getOut().setBody(entity.build());  

to:

exchange.getOut().setBody(entity.build().getContent());  

UPDATE

After your comment the other thing you can do is:

ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.build().writeTo(out);
InputStream  inputStream = new ByteArrayInputStream(out.toByteArray());
exchange.getOut().setBody(inputStream);


来源:https://stackoverflow.com/questions/28815792/apache-camel-multipart-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!