How the program implements FTP functionality in Java applications
Dalian Jie Tong Computer Technology Co., Ltd.
Wang Yu
----in Java programming, you may experience FTP programming, and this article demonstrates how to implement it.
----This procedure is developed by JBUILDER2.0, and in order to save space I will only list the main three parts. The Ftplist section is used to display files on the FTP server (with a slight figure). The Getbutton section is for sending a file from the FTP server. The Putbutton section uploads a file to the FTP server. Don't forget to introduce two library files (import Sun.net.*,import sun.net.ftp.*) in your program. The following are the Java source programs for these three sections.
----1) Displays the files on the FTP server
void ftplist_actionperformed (ActionEvent e) {
String Server=serveredit.gettext ();
//Input IP address of the FTP server
String User=useredit.gettext ();
//Login to the FTP server username
String Password=passwordedit.gettext ();
//Login to the FTP server username password
String Path=pathedit.gettext ();
//ftp The path on the server
try {
ftpclient ftpclient=new ftpclient ();
//Create FtpClient Object
ftpclient.openserver (server);
//Connection FTP server
ftpclient.login (user, password);
//Login to FTP server
if (path.length ()!=0) ftpclient.cd (path);
Telnetinputstream is=ftpclient.list ();
int C;
while ((C=is.read ())!=-1) {
System.out.print ((char) c);}
Is.close ();
ftpclient.closeserver ()//exit FTP server
} catch (IOException ex) {;}
}
2) Send a file from the FTP server
void getbutton_actionperformed (ActionEvent e) {
String Server=serveredit.gettext ();
String User=useredit.gettext ();
String Password=passwordedit.gettext ();
String Path=pathedit.gettext ();
String Filename=filenameedit.gettext ();
try {
ftpclient ftpclient=new ftpclient ();
ftpclient.openserver (server);
ftpclient.login (user, password);
if (path.length ()!=0) ftpclient.cd (path);
Ftpclient.binary ();
telnetinputstream is=ftpclient.get (filename);
file File_out=new file (filename);
FileOutputStream os=new
FileOutputStream (file_out);
byte[] Bytes=new byte[1024];
int C;
while ((C=is.read (bytes))!=-1) {
Os.write (BYTES,0,C);
}
Is.close ();
Os.close ();
Ftpclient.closeserver ();
} catch (IOException ex) {;}
}
3) Upload a file to the FTP server
void putbutton_actionperformed (ActionEvent e) {
String Server=serveredit.gettext ();
String User=useredit.gettext ();
String Password=passwordedit.gettext ();
String Path=pathedit.gettext ();
String Filename=filenameedit.gettext ();
try {
ftpclient ftpclient=new ftpclient ();
ftpclient.openserver (server);
ftpclient.login (user, password);
if (path.length ()!=0) ftpclient.cd (path);
ftpclient.binary ();
telnetoutputstream os=ftpclient.put (filename);
file File_in=new file (filename);
fileinputstream is=new FileInputStream (file_in);
byte[] Bytes=new byte[1024];
int C;
while ((C=is.read (bytes))!=-1) {
Os.write (bytes,0,c);}
Is.close ();
Os.close ();
Ftpclient.closeserver ();
The catch (IOException ex) {;}
}
}
----This program compiles and passes in the win95/98/nt,jbuilder2.0 environment.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.