資源簡介
網絡環境中的一項基本應用就是將文件從一臺計算機中復制到另一臺可能相距很遠的計算機中。而文件傳送協議FTP是因特網上使用得最廣泛的文件傳送協議。FTP使用客戶服務器方式。
設計要求:
1) 以命令行形式運行
2) 該FTP客戶端程序具有以下基本功能:能完成FTP鏈接的打開和關閉操作;能執行文件的上傳和下載;能完成目錄的創建、刪除等有關操作。要求可以傳輸簡單的文本文件。

代碼片段和文件信息
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:
//如果檢測到已經連接,返回ture??不再重新連接
if(checkFtpConnetion(ftpClient)){
break;
}else?{
// System.out.println(“輸入ftp地址:“);
// String?url?=?input.next();
//
// System.out.println(“請輸入端口號,默認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(“輸入遠程文件夾名字“);
String?folderPath?=?input.next();
System.out.println(“輸入遠程文件名“);
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(“請輸入創建目錄的路徑:“);
folderPath?=?input.next();
FtpUtil.createDirectory(ftpClient?folderPath);
break;
case?6:
System.out.println(“請輸入刪除類型:1、文件??2、文件夾“);
int?type?=?input.nextInt();
System.out.println(“請輸入要刪除的路徑:“);
String?remotePaths?=?input.next();
FtpUtil.delete(remotePaths?type?ftpClient);
break;
case?7:
boolean?flag?=?true;
while?(flag)?{
System.out.println(“請輸入字
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????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
- 上一篇:java遺傳算法_求TSP,java界面版
- 下一篇:Redis 連接池(升級)
評論
共有 條評論