【问题标题】:Apache Camel multipart routeApache Camel 多部分路由
【发布时间】: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


【解决方案1】:

当它说你需要一个 InputStream 时,消息很清楚

方法 build 返回一个 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());  

更新

在您发表评论后,您可以做的另一件事是:

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

【讨论】:

  • 感谢您的回复。我尝试了您的建议,但收到以下异常 java.lang.UnsupportedOperationException: Multipart form entity does not implement #getContent() 还有其他方法吗?
  • 现在我可以将请求发送到 API。但我认为它不包含实际文件。请参阅下面的 API 实现 public void handleFileUpload(MultipartHttpServletRequest request) { Iterator<String> itrator = request.getFileNames(); MultipartFile multipartFile = request.getFile(itrator.next()); } 当请求在这里接收 在这种情况下,itrator 没有提供文件进行迭代,所以我无法在我的服务器中获取文件。我可以在这里做什么?请帮忙
  • 我认为这是您构建 MultipartEntityBuilder 的方式。试试看是否可以在没有 Camel 的情况下工作:这里有一个关于如何使用它的示例:baeldung.com/httpclient-multipart-upload
猜你喜欢
  • 2016-09-29
  • 2018-07-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-27
  • 2018-02-14
  • 2014-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多