欧美特黄不卡,涩涩视频在线,成人国产精品一区二区网站,亚洲一区二区三区欧美

當前位置:雨林木風下載站 > 技術開發教程 > 詳細頁面

在JAVA應用程序中如何完成FTP的技巧

在JAVA應用程序中如何完成FTP的技巧

更新時間:2022-04-26 文章作者:未知 信息來源:網絡 閱讀次數:

在JAVA的編程中,您也許會遇到FTP方面的編程,本文就來演示如何實現它。

---- 本程序是由JBUILDER2.0來開發的,為了節約篇幅我只列出主要的三個部份。FtpList 部分是用來顯示FTP服務器上的文件(附圖略)。GetButton部分為從FTP服務器下傳一個文件。PutButton 部分為向FTP服務器上傳一個文件。別忘了在程序中還要引入兩個庫文件(import sun.net.*,import sun.net.ftp.*)。以下是這三部分的JAVA源程序。

---- 1)顯示FTP服務器上的文件

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服務器上下傳一個文件


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服務器上上傳一個文件

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) {;}
}
}

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

主站蜘蛛池模板: 白城市| 钟祥市| 霸州市| 霍州市| 两当县| 府谷县| 衡阳市| 昌黎县| 房山区| 宜兰县| 肇庆市| 曲阳县| 新安县| 同江市| 滕州市| 清新县| 玉田县| 通江县| 安岳县| 淮滨县| 平远县| 海丰县| 休宁县| 宁安市| 霍城县| 永川市| 英山县| 宜都市| 偃师市| 白沙| 商丘市| 当涂县| 冷水江市| 嘉禾县| 寻甸| 玛沁县| 丰原市| 虹口区| 杭州市| 天峨县| 惠来县|