Uploading a csv file to ftp without creating a phy

2019-09-16 10:31发布

问题:

I want to create and send a csv file to a remote ftp server,i am using CSVWriter from Apache Commons.

At present, I am creating a local temp file, wrapping this file around an Inputstream, and then uploading this stream to remote ftp server using client.store(<InputStream>,<Filename>) method. The problem here is it is creating a local temp file every time it is executed.
I want to get rid of this and be able to create a stream, especially csv stream directly. My main idea is to avoid creating a local temp file.

回答1:

You can wrap a ByteArrayOutputStream in any kind of Writer, and use that Writer as an argument for the CSVWriter. That way you can extract a byte array, which can be used as an argument for an ByteArrayInputStream.

If it's a large amount of data, you can use a combination of PipedInputStream and PipedOutputStream instead of the ByteArray*Streams.



标签: java ftp stream