2015-03-02 119 views
1

我試圖通過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)

+0

我的回答可以幫助您? – Panchitoboy 2015-03-06 10:56:49

+0

@Panchitoboy是的我設法用HTTPClient在外部發布請求。非常感謝您的幫助... – 2015-03-09 18:03:09

回答

2

的信息是明確的,當它說,你需要一個InputStream

的方法構建返回HttpEntity。

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

您可以用該方法的getContent()嘗試

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

試圖改變自己:

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

到:

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

UPDATE

您的評論,你可以做其他的事情以後是:

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

感謝您的回覆。我嘗試了你的建議,但我得到以下異常'java.lang.UnsupportedOperationException:多部分表單實體沒有實現#getContent()'有沒有其他方法? – 2015-03-03 12:56:34

+0

我編輯了我的答案 – Panchitoboy 2015-03-03 13:09:48

+0

現在我可以將請求發送到API。但我認爲它不包含實際的文件。請參閱 以下的API實現'public void handleFileUpload(MultipartHttpServletRequest request){ Iterator itrator = request.getFileNames(); MultipartFile multipartFile = request.getFile(itrator.next()); }' 當請求在這裏收到時在這種情況下'itrator'不提供迭代的文件,所以我無法獲取我的服務器中的文件。我能在這裏做什麼?請幫忙 – 2015-03-04 17:43:45