Search code examples
javaftpapache-commons-net

Create a temporary file, then upload it using FTP (Java webapp)


Users of my web application have an option to start a process that generates a CSV file (populated by some data from a database) and uploads it to an FTP server (and another department will read the file from there). I'm just trying to figure out how to best implement this. I use commons net ftp functionality. It offers two ways to upload data to the FTP server:

  • storeFile(String remote, InputStream local)
  • storeFileStream(String remote)

It can take a while to generate all the CSV data so I think keeping a connection open the whole time (storeFileStream) would not be the best way. That's why I want to generate a temporary file, populate it and only then transfer it.

What is the best way to generate a temporary file in a webapp? Is it safe and recommended to use File.createTempFile?


Solution

  • As long as you don't create thousands of CSV files concurrently the upload-time doesn't matter from my point of view. Databases usually output the data row by row and if this is already the format you need for the CSV file I strongly recommend not to use temporary files at all - just do the conversion on-the-fly:

    Create an InputStream implementation that reads the database data row by row, converts it to CSV and publish the data via it's read() methods.

    BTW: You mentioned that the conversion is done by a web application and that it can take a long time - this can be problematic as the default web client has a timeout. Therefore the long lasting process should be better done by a background thread only triggered by the webapp interface.