Java語言實(shí)現(xiàn)簡單FTP軟件 輔助功能模塊FTP站點(diǎn)管理實(shí)現(xiàn)(12)
本文為大家分享了輔助功能模塊FTP站點(diǎn)管理的實(shí)現(xiàn)方法,供大家參考,具體內(nèi)容如下
1、FTP站點(diǎn)管理
點(diǎn)擊“FTP站點(diǎn)管理”按鈕,彈出對話框“FTP站點(diǎn)管理”,如下圖
1) 連接站點(diǎn)
在FTP站點(diǎn)管理面板上選好要連接的站點(diǎn),點(diǎn)擊“連接”按鈕,則會將主機(jī)地址、端口號、用戶名好,并將密碼清空,如下圖
到其主要代碼如下
if (command.equals("link")) { // 如果單擊的是連接按鈕 frame.setLinkInfo(bean); // 調(diào)用setLinkInfo()方法 dispose(); // 關(guān)閉FTP站點(diǎn)管理對話框 }
其中調(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è)置端口號 userTextField.setText(bean.getUserName()); // 設(shè)置用戶名 PassField.setText(""); // 密碼清空 PassField.requestFocus(); // 密碼框請求焦點(diǎn)
2) 添加站點(diǎn)
在FTP站點(diǎn)管理面板上點(diǎn)擊“添加”按鈕,會產(chǎn)生一個新的對話框“添加FTP站點(diǎn)”,如下圖
填寫好站點(diǎn)名稱、地址、端口號、登陸用戶后點(diǎn)擊”確定”或者”重置”按鈕會觸發(fā)com.oyp.ftp.panel.manager.SiteDialog類的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(); // 判斷是否填寫了全部文本框 if (siteName.isEmpty() || server.isEmpty() || userName.isEmpty() || portStr.isEmpty()) { JOptionPane.showMessageDialog(this, "請?zhí)顚懭啃畔?); return; } int port = Integer.valueOf(portStr); // 創(chuàng)建FTP站點(diǎn)信息的JavaBean對象 SiteInfoBean bean = new SiteInfoBean(siteName, server, port, userName); // 如果對話框的siteBean不為空 if (siteBean != null) bean.setId(siteBean.getId()); // 設(shè)置FTP站點(diǎn)的ID編號 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, "請正確填寫端口號信息"); ex.printStackTrace(); return; } } if (command.equals("cancel")) { // 如果是重置按鈕 if (siteBean == null) // 如果對話框的siteBean屬性為空 clearInput(); // 調(diào)用清除文本框內(nèi)容的方法 else // 否則 initInput(); // 初始化界面文本框內(nèi)容 } }
3) 編輯站點(diǎn)
在FTP站點(diǎn)管理面板上選好要編輯的站點(diǎn),點(diǎn)擊“編輯”按鈕,會產(chǎn)生一個新的對話框“編輯FTP站點(diǎn)”,如下圖
編輯寫好站點(diǎn)名稱、地址、端口號、登陸用戶后點(diǎn)擊”確定”或者”重置”按鈕觸發(fā)com.oyp.ftp.panel.manager.SiteDialog類的actionPerformed(ActionEvent e)方法,其代碼如添加站點(diǎn)里一樣。
4) 刪除站點(diǎn)
在FTP站點(diǎn)管理面板上選好要刪除的站點(diǎn),點(diǎn)擊“刪除”按鈕,調(diào)用delSite(SiteInfoBean bean)方法,其代碼如下
/** * 刪除FTP站點(diǎn)的方法 */ public void delSite(SiteInfoBean bean) { // 從站點(diǎn)屬性集合對象中移除指定ID編號的站點(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方法存儲站點(diǎn)屬性 loadSiteList(); // 重新裝載站點(diǎn)列表 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
2、上傳/下載任務(wù)結(jié)束后自動關(guān)機(jī)
在com.oyp.ftp.panel.queue.QueuePanel類的refreshQueue()方法里會判斷任務(wù)隊(duì)列是否為空以及自動關(guān)機(jī)按鈕是否被按下,如果滿足條件則執(zhí)行系統(tǒng)關(guān)機(jī)命令,延遲30秒后自動關(guān)機(jī)。其代碼如下
/** * 刷新隊(duì)列的方法 */ private synchronized void refreshQueue() { // 如果自動關(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ù)模型對象 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)化托盤
當(dāng)點(diǎn)擊最小化軟件后,系統(tǒng)就會變成一個生成系統(tǒng)推盤,點(diǎn)擊系統(tǒng)托盤右鍵會有“顯示主窗體”和“退出”兩個菜單,如下圖
以下是初始化系統(tǒng)托盤的代碼,如下
/** * 初始化系統(tǒng)托盤的方法 */ private void initSystemTray() { if (SystemTray.isSupported()) systemTray = SystemTray.getSystemTray(); TrayIcon trayIcon = new TrayIcon(icon.getImage()); PopupMenu popupMenu = new PopupMenu("托盤菜單"); // 創(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(); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用Spring?Boot和JPA創(chuàng)建GraphQL?API
這篇文章主要介紹了利用Spring?Boot和JPA創(chuàng)建GraphQL?API,GraphQL既是API查詢語言,也是使用當(dāng)前數(shù)據(jù)執(zhí)行這些查詢的運(yùn)行時,下文更多相關(guān)內(nèi)容介紹需要的小伙伴可以參考一下2022-04-04mybatis?plus?MetaObjectHandler?不生效的解決
今天使用mybatis-plus自動為更新和插入操作插入更新時間和插入時間,配置了MetaObjectHandler不生效,本文就來解決一下,具有一定的 參考價值,感興趣的可以了解一下2023-10-10基于java查找并打印輸出字符串中字符出現(xiàn)次數(shù)
這篇文章主要介紹了基于java查找并打印輸出字符串中字符出現(xiàn)次數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11使用Spring實(shí)現(xiàn)@Value注入靜態(tài)字段
這篇文章主要介紹了使用Spring實(shí)現(xiàn)@Value注入靜態(tài)字段方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05詳解使用Spring Security進(jìn)行自動登錄驗(yàn)證
本篇文章主要介紹了詳解使用Spring Security進(jìn)行自動登錄驗(yàn)證,非常具有實(shí)用價值,需要的朋友可以參考下2017-09-09創(chuàng)建Maven項(xiàng)目和Spring IOC實(shí)例過程解析
這篇文章主要介紹了創(chuàng)建Maven項(xiàng)目和Spring IOC實(shí)例過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12