- 相關(guān)推薦
輔助功能模塊FTP站點(diǎn)管理實(shí)現(xiàn)方法
本文介紹了Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟,輔助功能模塊FTP站點(diǎn)管理的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。想了解更多相關(guān)信息請(qǐng)持續(xù)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!
1、FTP站點(diǎn)管理
點(diǎn)擊“FTP站點(diǎn)管理”按鈕,彈出對(duì)話(huà)框“FTP站點(diǎn)管理”,如下圖
1) 連接站點(diǎn)
在FTP站點(diǎn)管理面板上選好要連接的站點(diǎn),點(diǎn)擊“連接”按鈕,則會(huì)將主機(jī)地址、端口號(hào)、用戶(hù)名好,并將密碼清空,如下圖
到其主要代碼如下
if (command.equals("link")) { // 如果單擊的是連接按鈕
frame.setLinkInfo(bean); // 調(diào)用setLinkInfo()方法
dispose(); // 關(guān)閉FTP站點(diǎn)管理對(duì)話(huà)框
}
其中調(diào)用的是com.oyp.ftp.FTPClientFrame的setLinkInfo(SiteInfoBean bean)方法,其代碼如下
/**
* 設(shè)置FTP連接信息的方法,由FTP站點(diǎn)管理器調(diào)用
*/
ublic void setLinkInfo(SiteInfoBean bean) {
serverTextField.setText(bean.getServer()); // 設(shè)置主機(jī)地址
portTextField.setText(bean.getPort() + ""); // 設(shè)置端口號(hào)
userTextField.setText(bean.getUserName()); // 設(shè)置用戶(hù)名
PassField.setText(""); // 密碼清空
PassField.requestFocus(); // 密碼框請(qǐng)求焦點(diǎn)
2) 添加站點(diǎn)
在FTP站點(diǎn)管理面板上點(diǎn)擊“添加”按鈕,會(huì)產(chǎn)生一個(gè)新的對(duì)話(huà)框“添加FTP站點(diǎn)”,如下圖
填寫(xiě)好站點(diǎn)名稱(chēng)、地址、端口號(hào)、登陸用戶(hù)后點(diǎn)擊”確定”或者”重置”按鈕會(huì)觸發(fā)com.oyp.ftp.panel.manager.SiteDialog類(lèi)的actionPerformed(ActionEvent e)方法,其代碼如下
/**
* 界面按鈕的事件處理方法
*/
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand(); // 獲取按鈕的command屬性
if (command.equals("ok")) { // 如果是確定按鈕
try {
if (dialog == null) {
dispose();
return;
}
// 獲取界面所有文本框的內(nèi)容
String siteName = siteNameField.getText().trim();
String server = siteAddressField.getText().trim();
String userName = loginUserField.getText().trim();
String portStr = portField.getText().trim();
// 判斷是否填寫(xiě)了全部文本框
if (siteName.isEmpty() || server.isEmpty()
|| userName.isEmpty() || portStr.isEmpty()) {
JOptionPane.showMessageDialog(this, "請(qǐng)?zhí)顚?xiě)全部信息");
return;
}
int port = Integer.valueOf(portStr);
// 創(chuàng)建FTP站點(diǎn)信息的JavaBean對(duì)象
SiteInfoBean bean = new SiteInfoBean(siteName, server, port,
userName);
// 如果對(duì)話(huà)框的siteBean不為空
if (siteBean != null)
bean.setId(siteBean.getId()); // 設(shè)置FTP站點(diǎn)的ID編號(hào)
dialog.addSite(bean); // 調(diào)用父窗體的 addSite方法添加站點(diǎn)
dialog.loadSiteList(); // 調(diào)用父窗體的loadSiteList方法重載站點(diǎn)列表
dispose();
} catch (NullPointerException ex) {
ex.printStackTrace();
return;
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "請(qǐng)正確填寫(xiě)端口號(hào)信息");
ex.printStackTrace();
return;
}
}
if (command.equals("cancel")) { // 如果是重置按鈕
if (siteBean == null) // 如果對(duì)話(huà)框的siteBean屬性為空
clearInput(); // 調(diào)用清除文本框內(nèi)容的方法
else
// 否則
initInput(); // 初始化界面文本框內(nèi)容
}
}
3) 編輯站點(diǎn)
在FTP站點(diǎn)管理面板上選好要編輯的站點(diǎn),點(diǎn)擊“編輯”按鈕,會(huì)產(chǎn)生一個(gè)新的對(duì)話(huà)框“編輯FTP站點(diǎn)”,如下圖
編輯寫(xiě)好站點(diǎn)名稱(chēng)、地址、端口號(hào)、登陸用戶(hù)后點(diǎn)擊”確定”或者”重置”按鈕觸發(fā)com.oyp.ftp.panel.manager.SiteDialog類(lèi)的actionPerformed(ActionEvent e)方法,其代碼如添加站點(diǎn)里一樣。
4) 刪除站點(diǎn)
在FTP站點(diǎn)管理面板上選好要?jiǎng)h除的站點(diǎn),點(diǎn)擊“刪除”按鈕,調(diào)用delSite(SiteInfoBean bean)方法,其代碼如下
/**
* 刪除FTP站點(diǎn)的方法
*/
public void delSite(SiteInfoBean bean) {
// 從站點(diǎn)屬性集合對(duì)象中移除指定ID編號(hào)的站點(diǎn)屬性
siteInfo.remove(bean.getId());
try {
// 獲取站點(diǎn)屬性文件的輸出流
FileOutputStream out = new FileOutputStream(FILE);
siteInfo.store(out, "FTP站點(diǎn)數(shù)據(jù)"); // 調(diào)用store方法存儲(chǔ)站點(diǎn)屬性
loadSiteList(); // 重新裝載站點(diǎn)列表
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
2、上傳/下載任務(wù)結(jié)束后自動(dòng)關(guān)機(jī)
在com.oyp.ftp.panel.queue.QueuePanel類(lèi)的refreshQueue()方法里會(huì)判斷任務(wù)隊(duì)列是否為空以及自動(dòng)關(guān)機(jī)按鈕是否被按下,如果滿(mǎn)足條件則執(zhí)行系統(tǒng)關(guān)機(jī)命令,延遲30秒后自動(dòng)關(guān)機(jī)。其代碼如下
/**
* 刷新隊(duì)列的方法
*/
private synchronized void refreshQueue() {
// 如果自動(dòng)關(guān)機(jī)按鈕被按下并且上傳和下載的隊(duì)列都有任務(wù)
if (frame.getShutdownButton().isSelected() && localQueue.isEmpty()
&& ftpQueue.isEmpty()) {
try {
// 執(zhí)行系統(tǒng)關(guān)機(jī)命令,延遲30秒鐘
Runtime.getRuntime().exec("shutdown -s -t 30");
} catch (IOException e) {
e.printStackTrace();
}
}
// 創(chuàng)建表格的數(shù)據(jù)模型對(duì)象
DefaultTableModel model = new DefaultTableModel(columns, 0);
// 獲取本地上傳隊(duì)列中的任務(wù)
Object[] localQueueArray = localQueue.toArray();
// 遍歷本地上傳任務(wù)
for (int i = 0; i < localQueueArray.length; i++) {
Object[] queueValue = (Object[]) localQueueArray[i];
if (queueValue == null)
continue;
File localFile = (File) queueValue[0];
// 把上傳隊(duì)列的任務(wù)添加到表格組件的數(shù)據(jù)模型中
model.addRow(new Object[] { localFile.getAbsoluteFile(), "上傳",ftpClient.getServer(), i == 0 ? "正在上傳" : "等待上傳" });
}
// 獲取下載隊(duì)列的任務(wù)
Object[] ftpQueueArray = ftpQueue.toArray();
// 遍歷下載隊(duì)列
for (int i = 0; i < ftpQueueArray.length; i++) {
Object[] queueValue = (Object[]) ftpQueueArray[i];
if (queueValue == null)
continue;
FtpFile ftpFile = (FtpFile) queueValue[0];
// 把下載隊(duì)列的任務(wù)添加到表格組件的數(shù)據(jù)模型中
model.addRow(new Object[] { ftpFile.getAbsolutePath(), "下載",
ftpClient.getServer(), i == 0 ? "正在下載" : "等待下載" });
}
queueTable.setModel(model); // 設(shè)置表格使用本方法的表格數(shù)據(jù)模型
}
3、軟件系統(tǒng)化托盤(pán)
當(dāng)點(diǎn)擊最小化軟件后,系統(tǒng)就會(huì)變成一個(gè)生成系統(tǒng)推盤(pán),點(diǎn)擊系統(tǒng)托盤(pán)右鍵會(huì)有“顯示主窗體”和“退出”兩個(gè)菜單,如下圖
以下是初始化系統(tǒng)托盤(pán)的代碼,如下
/**
* 初始化系統(tǒng)托盤(pán)的方法
*/
private void initSystemTray() {
if (SystemTray.isSupported())
systemTray = SystemTray.getSystemTray();
TrayIcon trayIcon = new TrayIcon(icon.getImage());
PopupMenu popupMenu = new PopupMenu("托盤(pán)菜單");
// 創(chuàng)建顯示主窗體菜單項(xiàng)
MenuItem showMenuItem = new MenuItem("顯示主窗體");
showMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
FTPClientFrame.this.setExtendedState(JFrame.NORMAL);
FTPClientFrame.this.setVisible(true);
}
});
// 創(chuàng)建退出菜單項(xiàng)
MenuItem exitMenuItem = new MenuItem("退出");
exitMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
popupMenu.add(showMenuItem);
popupMenu.addSeparator();
popupMenu.add(exitMenuItem);
trayIcon.setPopupMenu(popupMenu);
try {
systemTray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
}
【輔助功能模塊FTP站點(diǎn)管理實(shí)現(xiàn)方法】相關(guān)文章:
php實(shí)現(xiàn)通過(guò)ftp上傳文件07-07
php使用ftp函數(shù)實(shí)現(xiàn)簡(jiǎn)單上傳功能10-31
實(shí)現(xiàn)員工“職業(yè)化”管理的方法11-01
Linux中ftp連接530錯(cuò)誤的解決方法07-31
無(wú)需重新編譯php加入ftp擴(kuò)展的方法09-04
Dreamweaver檢查站點(diǎn)鏈接錯(cuò)誤的方法06-24