-
FtpList部分是用來顯示FTP伺服器上的檔案;
GetButton部分為從FTP伺服器下傳一個檔案;
PutButton部分為向FTP伺服器上傳一個檔案。
別忘了程式中還要引入兩個庫檔案(import sun.net.*,import sun.net.ftp.*)。
以下是這三部分的JAVA來源程式:
1.顯示FTP伺服器上的文件
view plaincopy to clipboardprint?
void ftpList_actionPerformed(ActionEvent e) {
String server=serverEdit.getText();
//輸入的FTP伺服器的IP位址
String user=userEdit.getText();
//登入FTP伺服器的使用者名稱
String password=passwordEdit.getText();
//登入FTP伺服器的使用者名稱的口令
String path=pathEdit.getText();
//FTP伺服器上的路徑
try {
FtpClient ftpClient=new FtpClient();
//建立FtpClient對象
ftpClient.openServer(server);
//連接FTP伺服器
ftpClient.login(user, password);
//登入FTP伺服器
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();//退出FTP伺服器
} catch (IOException ex) {;}
}
void ftpList_actionPerformed(ActionEvent e) {
String server=serverEdit.getText();
//輸入的FTP伺服器的IP位址
String user=userEdit.getText();
//登入FTP伺服器的使用者名稱
String password=passwordEdit.getText();
//登入FTP伺服器的使用者名稱的口令
String path=pathEdit.getText();
//FTP伺服器上的路徑
try {
FtpClient ftpClient=new FtpClient();
//建立FtpClient對象
ftpClient.openServer(server);
//連接FTP伺服器
ftpClient.login(user, password);
//登入FTP伺服器
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();//退出FTP伺服器} catch (IOException ex) {;}
}
2.從FTP伺服器上下傳一個文件
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.向FTP伺服器上傳一個文件
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) {;}
}
}
本文出自CSDN博客,轉載請標示出處: http://blog.csdn.net/yan_auvtech/archive/2009/12/30/5104068.aspx
-