資源簡介
網(wǎng)絡(luò)環(huán)境中的一項(xiàng)基本應(yīng)用就是將文件從一臺(tái)計(jì)算機(jī)中復(fù)制到另一臺(tái)可能相距很遠(yuǎn)的計(jì)算機(jī)中。而文件傳送協(xié)議FTP是因特網(wǎng)上使用得最廣泛的文件傳送協(xié)議。FTP使用客戶服務(wù)器方式。
設(shè)計(jì)要求:
1) 以命令行形式運(yùn)行
2) 該FTP客戶端程序具有以下基本功能:能完成FTP鏈接的打開和關(guān)閉操作;能執(zhí)行文件的上傳和下載;能完成目錄的創(chuàng)建、刪除等有關(guān)操作。要求可以傳輸簡單的文本文件。

代碼片段和文件信息
package?cn.edu.scau.cmi.edwincheng.ftpServer;
import?java.text.SimpleDateFormat;
import?java.util.Scanner;
import?com.enterprisedt.net.ftp.FileTransferClient;
public?class?Client?{
/**
?*?代碼示例
?*?
?*?FileTransferClient?ftpClient?=
?*?FTPUnit.getFileTransferClient(“192.168.1.254“21“ftp““123456“);
?*?FileInputStream?fis?=?new?FileInputStream(new?File(“D:\\ip.log“));
?*?FileTransferOutputStream?ftos?=?ftpClient.uploadStream(“/test/ip.log“);
?*?byte[]?bytes?=?new?byte[1024];?int?c;?while?((c?=?fis.read(bytes))?!=?-1)
?*?{?ftos.write(bytes?0?c);?}?ftos.flush();?ftos.close();?fis.close();
?*?
?*?@throws?IOException?
?*?@throws?FTPException?
?*?
?*/
public?static?SimpleDateFormat?df?=?new?SimpleDateFormat(“yyyy-MM-dd?HH:mm:ss“);
public?static?java.util.Date?time?=?new?java.util.Date();
public?static?void?main(String[]?args)?throws?Exception?{
int?option;
boolean?runFlag?=?true;
Scanner?input?=?new?Scanner(System.in);
//ftp類
FileTransferClient?ftpClient?=?null?;
while?(runFlag)?{
showmenu();
option?=?input.nextInt();
switch?(option)?{
case?1:
//如果檢測到已經(jīng)連接,返回ture??不再重新連接
if(checkFtpConnetion(ftpClient)){
break;
}else?{
// System.out.println(“輸入ftp地址:“);
// String?url?=?input.next();
//
// System.out.println(“請輸入端口號(hào),默認(rèn)21:“);
// int?port?=?input.nextInt();
//
// System.out.println(“輸入用戶名:“);
// String?username?=?input.next();
//
// System.out.println(“輸入密碼:“);
// String?password?=?input.next();
//
// ftpClient?=?FtpUtil.getFileTransferClient(url?username?password?port);
ftpClient?=?FtpUtil.getFileTransferClient(“10.211.55.5“?“private“?“private“?21);
// ftpClient?=?FtpUtil.getFileTransferClient(“172.26.14.35“?“upload“?“upload“?21);
}
break;
case?2:
ftpClient=FtpUtil.closeFileTransferClient(ftpClient);
break;
case?3:
System.out.println(“輸入本地文件路徑:“);
String?localFilePath?=?input.next();
System.out.println(“輸入遠(yuǎn)程文件夾名字“);
String?folderPath?=?input.next();
System.out.println(“輸入遠(yuǎn)程文件名“);
String?remoteFileName?=?input.next();
FtpUtil.upload(localFilePath?remoteFileName?folderPath?ftpClient);
System.out.println(“上傳成功“);
break;
case?4:
System.out.println(“請輸入下載文件路徑:“);
String?remoteFilePath?=?input.next();
System.out.println(“請輸入本地文件路徑:“);
localFilePath?=?input.next();
FtpUtil.download(localFilePath?remoteFilePath?ftpClient);
break;
case?5:
System.out.println(“請輸入創(chuàng)建目錄的路徑:“);
folderPath?=?input.next();
FtpUtil.createDirectory(ftpClient?folderPath);
break;
case?6:
System.out.println(“請輸入刪除類型:1、文件??2、文件夾“);
int?type?=?input.nextInt();
System.out.println(“請輸入要?jiǎng)h除的路徑:“);
String?remotePaths?=?input.next();
FtpUtil.delete(remotePaths?type?ftpClient);
break;
case?7:
boolean?flag?=?true;
while?(flag)?{
System.out.println(“請輸入字
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-09-09?20:57??FTPClient\
?????文件?????????414??2017-04-25?22:51??FTPClient\.classpath
?????文件?????????368??2017-04-25?17:28??FTPClient\.project
?????目錄???????????0??2017-04-25?22:51??FTPClient\.settings\
?????文件?????????587??2017-04-25?22:50??FTPClient\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2017-04-25?22:51??FTPClient\bin\
?????目錄???????????0??2017-04-25?22:51??FTPClient\bin\cn\
?????目錄???????????0??2017-04-25?22:51??FTPClient\bin\cn\edu\
?????目錄???????????0??2017-04-25?22:51??FTPClient\bin\cn\edu\scau\
?????目錄???????????0??2017-04-25?22:51??FTPClient\bin\cn\edu\scau\cmi\
?????目錄???????????0??2017-04-25?22:51??FTPClient\bin\cn\edu\scau\cmi\edwincheng\
?????目錄???????????0??2017-04-25?23:27??FTPClient\bin\cn\edu\scau\cmi\edwincheng\ftpServer\
?????文件????????5875??2017-05-02?00:42??FTPClient\bin\cn\edu\scau\cmi\edwincheng\ftpServer\Client.class
?????文件????????5266??2017-05-02?12:59??FTPClient\bin\cn\edu\scau\cmi\edwincheng\ftpServer\FtpUtil.class
?????文件?????????514??2017-05-02?13:11??FTPClient\README.txt
?????目錄???????????0??2017-09-09?20:57??__MACOSX\
?????目錄???????????0??2017-09-09?20:57??__MACOSX\FTPClient\
?????文件?????????632??2017-05-02?13:11??__MACOSX\FTPClient\._README.txt
?????目錄???????????0??2017-04-25?17:29??FTPClient\src\
?????目錄???????????0??2017-04-25?17:29??FTPClient\src\cn\
?????目錄???????????0??2017-04-25?17:29??FTPClient\src\cn\edu\
?????目錄???????????0??2017-04-25?17:29??FTPClient\src\cn\edu\scau\
?????目錄???????????0??2017-04-25?17:29??FTPClient\src\cn\edu\scau\cmi\
?????目錄???????????0??2017-04-25?17:29??FTPClient\src\cn\edu\scau\cmi\edwincheng\
?????目錄???????????0??2017-04-25?23:27??FTPClient\src\cn\edu\scau\cmi\edwincheng\ftpServer\
?????文件????????6883??2017-05-02?00:42??FTPClient\src\cn\edu\scau\cmi\edwincheng\ftpServer\Client.java
?????文件????????5506??2017-05-02?12:59??FTPClient\src\cn\edu\scau\cmi\edwincheng\ftpServer\FtpUtil.java
評論
共有 條評論