-
The FtpList part is used to display files on the FTP server;
The GetButton part is to download a file from the FTP server;
The PutButton part is to upload a file to the FTP server.
Don't forget to introduce two library files (import sun.net.*, import sun.net.ftp.*) into the program.
The following is the JAVA source program for these three parts:
1. Display files on FTP server
view plaincopy to clipboardprint?
void ftpList_actionPerformed(ActionEvent e) {
String server=serverEdit.getText();
//Enter the IP address of the FTP server
String user=userEdit.getText();
//Username to log in to the FTP server
String password=passwordEdit.getText();
//Password of the username to log in to the FTP server
String path=pathEdit.getText();
//Path on the FTP server
try {
FtpClient ftpClient=new FtpClient();
//Create FtpClient object
ftpClient.openServer(server);
//Connect to FTP server
ftpClient.login(user, password);
//Log in to the 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 the FTP server
} catch (IOException ex) {;}
}
void ftpList_actionPerformed(ActionEvent e) {
String server=serverEdit.getText();
//Enter the IP address of the FTP server
String user=userEdit.getText();
//Username to log in to the FTP server
String password=passwordEdit.getText();
//Password of the username to log in to the FTP server
String path=pathEdit.getText();
//Path on the FTP server
try {
FtpClient ftpClient=new FtpClient();
//Create FtpClient object
ftpClient.openServer(server);
//Connect to FTP server
ftpClient.login(user, password);
//Log in to the 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 the FTP server} catch (IOException ex) {;}
}
2. Upload and download a file from the FTP server
view plaincopy to clipboardprint?
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) {;}
}
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
view plaincopy to clipboardprint?
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();
} catch (IOException ex) {;}
}
}
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/yan_auvtech/archive/2009/12/30/5104068.aspx
-