|
-
Oct 10th, 2002, 07:55 AM
#1
Thread Starter
Hyperactive Member
FTP in console app
Anyone know where I can find some examples of how to FTP (pull from a server only, no uploading) a file via java in a console app?
Thanks!
Morgan
-
Oct 10th, 2002, 12:16 PM
#2
Hyperactive Member
This is not hard at all. Check out sun's ftp client. It is not documented by sun at all by you can find some info on it at the following:
http://www.informatik.tu-freiberg.de...colClient.html
http://www.aoindustries.com/docs/aoc...FtpClient.html
here is an example that shows how to upload and download.
Code:
import sun.net.*;
import sun.net.ftp.*;
import java.io.*;
public class ftp_utility {
public ftp_utility () {
FtpClient fcMyFtp = new FtpClient();
try {
int ch;
fcMyFtp.openServer("myftpsite");
fcMyFtp.login("myname","mypassword");
TelnetInputStream tisList =
fcMyFtp.list();
while( (ch = tisList.read()) != -1 )
System.out.print((char)ch);
// ... other stuff.
fcMyFtp.cd("temp");
fcMyFtp.binary();
// writing a file:
FileInputStream fis = new
FileInputStream( "/home/gadio/temp/tempfile" );
TelnetOutputStream tos =
fcMyFtp.put("tempfile.new");
byte buffer[] = new byte[1000];
int len;
while( (len = fis.read(buffer)) != -1 ) {
tos.write(buffer,0,len);
}
fis.close();
tos.close();
// reading a file:
FileOutputStream fos = new
FileOutputStream( "/home/gadio/temp/tempfi
le.read" );
TelnetInputStream tis =
fcMyFtp.get("tempfile.new");
while( (len = tis.read(buffer)) != -1 ) {
fos.write(buffer,0,len);
}
tis.close();
fos.close();
} catch( IOException e ) {
e.printStackTrace();
}
}
public static void main(String args[]) {
ftp_utility f = new ftp_utility();
}
}
Last edited by billrogers; Oct 10th, 2002 at 01:28 PM.
-
Oct 10th, 2002, 12:40 PM
#3
Hyperactive Member
another thing you could do is execute a runtime to use say the microsoft ftp client, but you will lose platform independence doing this which may not be bad if you know what platform you are always going to run on.
Last edited by billrogers; Oct 10th, 2002 at 02:24 PM.
-
Oct 10th, 2002, 01:35 PM
#4
Thread Starter
Hyperactive Member
That worked beautifully, thank you!
-
Oct 10th, 2002, 02:24 PM
#5
Hyperactive Member
your welcome, glad to be of help
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
|