如何将字节或流写入 Apache Camel FTP 以传输文件
How to write byte or stream to Apache Camel FTP to transfer file
在我目前的代码中,我从数据库中获取数据,然后从数据中写入一个文件。我有这种骆驼路线和工作解决方案:-
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 服务器。我不知道该怎么做?但我想应该可以做到。你能告诉我这可能吗?如果是,该怎么做?
你不应该使用 streamCaching 除非你真的使用它。它将您的文件存储在内存中,如果您需要消耗输入的倍数,请使用它。
您可以使用 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 文件的人。
我设法用另一种方式解决了这个问题。它更适合我的特定问题。
byte[] csvData = csvStringBuilder.toString().getBytes();
Routes.withProducer(producer)
.withHeader(Exchange.FILE_NAME, myCsvFile.csv)
.withBody(csvData)
.to(SFTP_SERVER).request(byte[].class);
是的,这是可能的 :) 要做到这一点,请在骆驼 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");
}
}
在我目前的代码中,我从数据库中获取数据,然后从数据中写入一个文件。我有这种骆驼路线和工作解决方案:-
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 服务器。我不知道该怎么做?但我想应该可以做到。你能告诉我这可能吗?如果是,该怎么做?
你不应该使用 streamCaching 除非你真的使用它。它将您的文件存储在内存中,如果您需要消耗输入的倍数,请使用它。
您可以使用 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 文件的人。
我设法用另一种方式解决了这个问题。它更适合我的特定问题。
byte[] csvData = csvStringBuilder.toString().getBytes();
Routes.withProducer(producer)
.withHeader(Exchange.FILE_NAME, myCsvFile.csv)
.withBody(csvData)
.to(SFTP_SERVER).request(byte[].class);
是的,这是可能的 :) 要做到这一点,请在骆驼 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");
}
}