|
-
March 31st, 2004, 10:41 AM
#1
TIP: Simple FTP server interaction
NOTE: THis post contains code that uses nonstandard classes from the sun.net hierarchies. It is not guaranteed that the required classes will be present on all, or any Java2 distribution, or that behaviour will be consistent across deployments of the J2SDK
-------
Intro:
While looking for a simple way to interact with an ftp server (i wasnt shifting hundreds of files) and not really wanting to write my own client I considered writing an extension to the URLConnection class, as HttpURLConnection exists, but Ftp apparently does not. (i also considered writing FTPFile and mimicking File for most things.. I may still)
Anyways, i dscovered that FtpURLCOnnection DOES exist, but it is not standard, probably because the darn thing is so hard to ratify.
I played with ti a while and decided to post this as a FYI for the community BUT ALSO TO RECEIVE SOME FEEDBACK from users who have maybe used this before, or have some ideas I have not tried.
code:
the following code snippet reads a file from an ftp server, and dumps it to the console (hint: try it with a SMALL binary or ascii file)
Code:
import java.net.*;
import java.io.InputStream;
public class FTPRead{
public static void main(String[] argv)throws Exception{
URL u = new URL("ftp://USER:PASS@HOST:PORT/PATH/TO/THEFILE");
URLConnection uc = u.openConnection() ;
uc.setDoInput(true);
InputStream in = uc.getInputStream() ;
byte[] buf = new byte[4096];
for(int br = in.read(buf); br > -1; br = in.read(buf)){
System.out.write(buf, 0 , br);
}
in.close();
}
}
it appears that the user/pass needs embedding in the URL; i know of no way to pass it, as the classes in use are nonstandard, and i do not know if they use the Properties at all.
There is virtually nothing you can do to protect this password either, if your source files are stolen.. So take care with this..
Some code to read a DIRECTORY
Code:
import java.net.*;
import java.io.InputStream;
public class FTPRead{
public static void main(String[] argv)throws Exception{
URL u = new URL("ftp://USER:PASS@HOST:PORT/PATH/TO/THE/DIR/");
URLConnection uc = u.openConnection() ;
uc.setDoInput(true);
InputStream in = uc.getInputStream() ;
byte[] buf = new byte[4096];
for(int br = in.read(buf); br > -1; br = in.read(buf)){
System.out.write(buf, 0 , br);
}
in.close();
}
}
it appears that, if the url has a trailing slash, a directory is assumed, and a listing is provided in the following format:
-rwxrwxrwx 1 owner group 69414 Jan 15 6:01 030107cnd.exe
rwxrwxrwx = permissions for local user (first rwx), group (2nd rwx) and everyone else (third rwx)
when writing files, i have no iodea how to change these
any client you write should parse this output and decide what group of people it belongs to
the following code writes a file to the server:
Code:
import java.net.*;
import java.io.OutputStream;
public class FTPTest{
public static void main(String[] argv)throws Exception{
URL u = new URL("ftp://USER:PASS@HOST:PORT/PA/TH/TO/FI/LE/THEFILE") ;
URLConnection uc = u.openConnection() ;
uc.setDoOutput(true);
OutputStream out = uc.getOutputStream() ;
out.write("THIS DATA WILL BE WRITTEN TO FILE".getBytes());
out.close();
}
}
--
Now, can anyone tell me how comphehensive the FtpURLConnection is? does it default to binary transfer mode, 777 permissions, username and pass another way etc.. ?
Last edited by cjard; September 1st, 2004 at 06:27 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|