1

我首先有多部分文件,我想将它发送到骆驼管道并用原始名称保存这个文件。

我的代码:

@Autowired
ProducerTemplate producerTemplate;
...
      producerTemplate.sendBody("seda:rest_upload", multipartFile);

另一方面,我有:

from("seda:rest_upload").convertBodyTo(File.class).to("file://rest_files");

我也尝试注册转换器:

@Converter
public class MultiPartFileToFileConvertor {
    @Converter
    public static File toFile(MultipartFile multipartFile) throws IOException {
        File convFile = new File(multipartFile.getOriginalFilename());
        multipartFile.transferTo(convFile);
        return convFile;
    }
}

当我执行我的代码时,我看到以下堆栈跟踪:

Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
[route2            ] [route2            ] [seda://rest_upload                                                            ] [         3]
[route2            ] [convertBodyTo1    ] [convertBodyTo[java.io.File]                                                   ] [         2]

Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------

org.apache.camel.InvalidPayloadException: No body available of type: java.io.File but has value: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7 of type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile on: Message[ID-ntkachev-1509013331141-0-13]. Caused by: No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7. Exchange[ID-ntkachev-1509013331141-0-14]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7]
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:113) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.processor.ConvertBodyProcessor.process(ConvertBodyProcessor.java:91) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) [camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:138) [camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:101) [camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) [camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.component.seda.SedaConsumer.sendToConsumers(SedaConsumer.java:298) [camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.component.seda.SedaConsumer.doRun(SedaConsumer.java:210) [camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.component.seda.SedaConsumer.run(SedaConsumer.java:155) [camel-core-2.20.0.jar:2.20.0]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:206) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:111) ~[camel-core-2.20.0.jar:2.20.0]
    ... 12 common frames omitted

2017-10-26 13:30:58.447  WARN 19608 --- [a://rest_upload] o.a.camel.component.seda.SedaConsumer    : Error processing exchange. Exchange[]. Caused by: [org.apache.camel.InvalidPayloadException - No body available of type: java.io.File but has value: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7 of type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile on: Message[ID-ntkachev-1509013331141-0-13]. Caused by: No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7. Exchange[ID-ntkachev-1509013331141-0-14]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7]]

org.apache.camel.InvalidPayloadException: No body available of type: java.io.File but has value: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7 of type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile on: Message[ID-ntkachev-1509013331141-0-13]. Caused by: No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7. Exchange[ID-ntkachev-1509013331141-0-14]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7]
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:113) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.processor.ConvertBodyProcessor.process(ConvertBodyProcessor.java:91) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:138) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:101) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.component.seda.SedaConsumer.sendToConsumers(SedaConsumer.java:298) [camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.component.seda.SedaConsumer.doRun(SedaConsumer.java:210) [camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.component.seda.SedaConsumer.run(SedaConsumer.java:155) [camel-core-2.20.0.jar:2.20.0]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:206) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:111) ~[camel-core-2.20.0.jar:2.20.0]
    ... 12 common frames omitted

如何解决这个问题?

4

2 回答 2

2

使用 SpringBoot 的类型转换器示例,将使用 Camel 2.23.0 或更高版本自动拾取。

@Component
public class MyPackageTypeConverter implements TypeConverters {

  private final ObjectMapper mapper;

  @Autowired
  public MyPackageTypeConverter(ObjectMapper mapper) {
    this.mapper = mapper;
  }

  @Converter
  public byte[] myPackageToByteArray(MyPackage source) {
    try {
      return mapper.writeValueAsBytes(source);
    } catch (JsonProcessingException e) {
      throw new RuntimeException(e);
    }
  }

  @Converter
  public MyPackage byteArrayToMyPackage(byte[] source) {
    try {
      return mapper.readValue(source, MyPackage.class);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

}
于 2019-01-23T00:04:27.147 回答
1

您必须在 META-INF 目录中添加名为 TypeConverter 的服务发现文件。

src\main\resources\META-INF\services\org\apache\camel\TypeConverter 

此文件包含 @Converter 类的完全限定名称。

例如

mypackage.MultiPartFileToFileConvertor 

另请参阅 Github https://github.com/camelinaction/camelinaction2/tree/b6a43abf9e0d4ec4e3753ebd735bb3448f98194b/chapter3/converter中 Camel in action 2 中的示例。

本书第 3.6 章还详细解释了自定义转换器的发现和加载如何工作。

于 2017-10-26T13:25:47.523 回答