Showing posts with label FTP. Show all posts
Showing posts with label FTP. Show all posts

Friday, January 21, 2011

FTP in Java to an IFS folder

A couple of years ago I wrote about uploading files via FTP to a remote server, ending the post reminding about the Commons Net library. Well, the time has come for me to use it, having to upload a file to an IFS folder.

The naive approach didn't work:
@Test
public void basicTest() throws Exception {
URL url = new URL("ftp://user:pass@remoteserver/path/to/remote/folder/" + "outputFile.txt");
URLConnection urlc = url.openConnection();
OutputStream outputStream = urlc.getOutputStream();

String text = "oooooo sooooleee miiiiiiiooooooo.....";
InputStreamReader reader = new InputStreamReader(IOUtils.toInputStream(text));

int mychar = reader.read();
while (mychar != -1) {
outputStream.write(mychar);
mychar = reader.read();
}
reader.close();
outputStream.close();
}
as it returned the "550-Specified library does not exist or cannot be accessed." error. Too bad. After some investigations we found that before we could access the folder we were interested in we had to issue the "cd /" command. All right, I went for the commons:
@Test
public void commonsTest() throws Exception {

FTPClient ftp = new FTPClient();
ftp.connect("remoteserver");
ftp.login("user", "pass");

ftp.changeWorkingDirectory("/");
ftp.changeWorkingDirectory("path/to/remote/folder");

OutputStream outputStream = ftp.storeFileStream("outputFile.txt");

String text = "oooooo sooooleee miiiiiiiooooooo.....";
InputStreamReader reader = new InputStreamReader(IOUtils.toInputStream(text));

int mychar = reader.read();
while (mychar != -1) {
outputStream.write(mychar);
mychar = reader.read();
}

reader.close();
outputStream.close();

ftp.logout();
ftp.disconnect();
}
Obviously this code is simplistic and pretends that errors cannot happen, but it's just to grab the sense of it. We create an instance of the FTPClient class and connect to our remote server (after all we are dealing with sockets... does that ring a bell?), then we use the normal FTP commands to reach the folder we need. After getting an OutputStream, obtained with the storeFileStream(String filename) method, we do everything like in the previous example, then log out and disconnect from the server.

Monday, January 12, 2009

FTP in Java

This one is a reminder as well; the example is not complete but I'm sure that it will be enough to get to the point.

FTP is not directly supported in Java, rather it is disguised behind a URLConnection. Once you've opened the connection you just access the appropriate stream and normally use it:

URL url = new URL("ftp://username:password@your.host/complete/path/to/remote/file");
URLConnection connection = url.openConnection();

To "get" a file you use the input stream:

InputStream is = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(is);

then you create a writer...

FileWriter writer = new FileWriter(new File("complete/path/to/local/file"));

and flush what you read:

int tmp;
while ((tmp = reader.read()) != -1) {
writer.write(tmp);
}

aren't we forgetting anything? oh yes, clean up the room:

reader.close();
writer.close();

The dual "put" operation requires an output stream:

OutputStream os = connection.getOutputStream();

and the core of the transfer loop would simply be

os.write(tmp);

Ok, the read-and-flush code is quite ugly, one would problably wrap the reader in a BufferedReader, append and flush instead of directly writing and stuff, but for version 0.1 it will suffice.

For most advanced needs it is always possible to use the Commons Net.

FTP on IIS

Just a reminder for my colleagues, no detailed stuff, so if a bunch of crackers enters your site I will not consider myself responsible :-)

To configure FTP access on a Windows host (assuming a default FTP server is already up and running):
  • Create an "FTPUsers" group, or something similar; this will contain all users that will access your preciousss files and folders (hiss as Gollum when you read this)
  • Decide which folder on your hard drive will contain the files... and, please, do not use the default one :-) This will be your ftp root. Give the FTPUsers group read permissions.
  • Create a user, remove it from the default "Users" group for added security and add it to the FTPUsers group.
  • Create a new folder in your ftp root; this will be the default folder for your freshly created user.
  • Give that user the appropriate NTFS read/write permissions on the folder
  • Create a new virtual directory in IIS pointing it to the folder you just created, naming it after your user; in the FTP Properties / Security Accounts tab disable the "Allow Anonymous Connections" checkbox.
  • Finally, test the connection... No TDD on this :-)
This reminds me of when I was younger, so much younger than today... at least at the time this was in my job description :-P