Retrieve FTP files directly to memory

The CFFTP tag includes extensive support for FTP operations. However, all remote file access requires interaction with the local drive. For example, if you want to retrieve and parse an XML file, you have to get the file and save to disk, then use cffile to load it into memory, and finally parse it. All this extra disk access takes unnecessary time.

Since CFMX is now Java based, we can take advantage of the Java network library to perform this same action without disk access.

The following code grabs a file from a local FTP site and displays it on screen.

s = "";

jUrl = createObject("java", "java.net.URL");
jUrl.init("ftp://127.0.0.1/temp/test.xml");

inStream = jUrl.openStream();

inRead = createObject(
"java",
"java.io.InputStreamReader");

inRead.init(inStream);

bufRead = createObject(
"java",
"java.io.BufferedReader");
bufRead.init(inRead);

#s#

I haven’t tried it but you should be able to specify the username and password within the url as in “ftp://username:password@hostname/path/filename”.

8 thoughts on “Retrieve FTP files directly to memory

  1. Know what would be even cooler? Make the UDF take any string. If you pass http or ftp whatever, it makes a http or ftp request. If you pass anything else, it considers it a local file path.

  2. The code will accept http:// right now, but in that respect it doesn’t have any real advantage over CFHTTP. Similar situation with local files, the change would be simple, but it wouldn’t have any real benefit over CFHTTP. Since it’s a CFScript implementation it would probably be slower than CFHTTP/CFFILE.

    What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>