【问题标题】:How to write byte or stream to Apache Camel FTP to transfer file如何将字节或流写入 Apache Camel FTP 以传输文件
【发布时间】:2018-11-30 07:17:43
【问题描述】:

在我目前的代码中,我从数据库中获取数据,然后从数据中写入一个文件。我有这种骆驼路线和可行的解决方案:-

private static final String INPUT_FILE_DIRECTORY_URI = "file:" + System.getProperty("user.home")
        + "/data/cdr/?noop=false";

private static final String SFTP_SERVER = "sftp://" +System.getProperty("user.name")
        + "@sftp_server_url/data/cdr/?privateKeyFile=~/.ssh/id_rsa&passiveMode=true";

from(INPUT_FILE_DIRECTORY_URI)
            .streamCaching()
            .log("Sending file to local sftp")
            .to(SFTP_SERVER); 

我不想在本地磁盘中写入文件。相反,我想将文件数据直接写入 SFTP 服务器。我不知道该怎么做?但我想应该可以做到。你能告诉我这可能吗?如果是,该怎么做?

【问题讨论】:

    标签: java stream byte filestream camel-ftp


    【解决方案1】:

    除非你真的使用它,否则你不应该使用它。它将您的文件存储在内存中,如果您需要多次使用您的输入,请使用它。

    您可以使用Jpa component 或自定义bean 获取您的数据。从数据库中加载它,然后将其发送到您的 ftp 服务器。

    与 Jpa :

    @Entity 
    @NamedQuery(name = "data", query = "select x from Data x where x.id = 1") 
    public class Data { ... }
    

    之后,您可以像这样定义一个消费者 uri:

    from("jpa://org.examples.Data?consumer.namedQuery=data")
    .to("SFTP_SERVER");
    

    编辑:将列表转换为 csv 并将其发送到 ftp:

    from("jpa://org.examples.Data?consumer.namedQuery=data")
    .marshal()
    .csv()
    .to("sftp://" +System.getProperty("user.name") + 
    "@sftp_server_url/data/cdr/myFile.csv?" +"privateKeyFile=~/.ssh/id_rsa&passiveMode=true");
    

    请参阅 CSV component 将列表转换为 csv 文件。

    【讨论】:

    • 谢谢。但实际上,我没有从数据库中获取我的数据。我从另一个来源得到它作为一个列表。它没有骆驼组件。所以问题应该是如何将列表写入 sftp 端点?列表数据应在 sftp 保存为 CSV 文件。
    • 你需要用 camel csv 组件编组你的列表,然后将它发送到你的 ftp 服务器。
    【解决方案2】:

    我设法以另一种方式解决了这个问题。它更适合我的特定问题。

                 byte[] csvData = csvStringBuilder.toString().getBytes();
                 Routes.withProducer(producer)
                        .withHeader(Exchange.FILE_NAME, myCsvFile.csv)
                        .withBody(csvData)
                        .to(SFTP_SERVER).request(byte[].class);
    

    【讨论】:

      【解决方案3】:

      是的,这是可能的 :) 要做到这一点,请在骆驼 DIRECT 组件中发送文件 inputStream,并在关联的路由中将副本复制到 FTP。我使用这种情况,上传文件并使用 from(directInputStreamName).to(yourFtpUri) 将其直接复制到 ftp。这是一个示例代码:

      您的服务

      @Service
      public class FileService {
        @Produce(uri = PfnumDownloadConstants.CAMEL_DIRECT_UPLOAD)
        private ProducerTemplate producer;
      
        public void sendFileToFtp(File fileToSend, String ftpDestinationUri) throws IOException {
          Map<String, Object> headers = new HashMap<>();
          //In this variable you can init the ftp destination uri or you can hard code it in route
          headers.put("destinationUri", ftpDestinationUri);
          //set filename to name your file in ftp
          headers.put(Exchange.FILE_NAME_ONLY, file.getName());
          InputStream targetStream = new FileInputStream(file);
          //send stream as body and list of headers to direct 
          producer.sendBodyAndHeaders(targetStream, headers);
        }
      }
      

      你的骆驼路线

      @Component
      public class FileUploadRoute extends RouteBuilder {
        @Override
        public void configure() throws Exception {
          //Manage camel exception in a dedicated processor
          onException(Exception.class).process(exceptionProcessor).log("error :: ${exception}");
        
          from(CAMEL_DIRECT_UPLOAD)
          .log("file copy to ftp '${header.CamelFileNameOnly}' in  process")
          .toD("file:/mnt?fileName=${header.CamelFileNameOnly}&delete=false")
          .log("copy done");
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-04
        • 2020-05-26
        • 2017-11-21
        • 1970-01-01
        • 2012-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多