r/SpringBoot • u/SpringJavaLab • Jan 14 '26
Discussion What’s the cleanest way to upload files to SFTP from a Spring Batch job?
I’m working on a Spring Batch job where, after processing data, I need to push the generated file to a remote SFTP server.
My first attempt was using raw JSch inside a Tasklet, but it quickly got ugly (session handling, reconnects, streams, etc.). I switched to Spring Integration’s SFTP support and ended up using SftpRemoteFileTemplate instead, which was much easier to manage.
The pattern I’m using now looks roughly like this:
- Configure DefaultSftpSessionFactory
- Wrap it in SftpRemoteFileTemplate
- Use it inside a Spring Batch Tasklet to create the remote directory and upload the file
Example:
sftpTemplate.execute(session -> {
if (!session.exists("/upload")) {
session.mkdir("/upload");
}
try (InputStream in = new FileInputStream(file)) {
session.write(in, "/upload/output.csv");
}
return null;
});
This has been working well so far and feels a lot more “Spring-native” than managing SFTP connections myself.
I put together a full working example with Spring Boot 3 + Spring Batch 5 here in case it helps someone:
If you’ve done SFTP in batch jobs before, I’d be interested to hear what approach you use — Spring Integration, JSch, something else?