Upload a file to an SFTP server using PDFBox save method without storing the file to the local system?



I’m trying to save the edited PDF which I fetched from the remote server back to its location without having it downloaded/stored on the local machine. I’m using JSch SFTP method to get the input PDF file from the SFTP server using

x = new BufferedInputStream(channelSftp.get("example.pdf"));
PDDocument document = PDDocument.load(x);

and after doing some edits using PDFbox, I’m trying to save it using:

documents.save(new File("ftp/path/location"));

I am not able to because I know it only works for your local directory only. Also I can see that document.save accept OutputStream` parameter, but I do not know how to use it here.

I don’t have any problems with taking input using stream reader. All I need is to save that edited PDF back to its location (possibly replace) without having to download it on my local system.

Answer

Use the ChannelSftp.put overload that returns OutputStream:

try (OutputStream out = channelSftp.put("example.pdf")) {
    documents.save(out);
}


Source: stackoverflow