本文為大家介紹了ftp上傳下載隊列窗口的實現方法,供大家參考,具體內容如下
1、首先看一下隊列窗口的界面
2、看一下上傳隊列窗口的界面
3、看一下下載隊列窗口的界面
package com.oyp.ftp.panel.queue; import static java.awt.borderlayout.center; import static java.awt.borderlayout.east; import static javax.swing.listselectionmodel.single_selection; import java.awt.borderlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.file; import java.io.ioexception; import java.util.linkedlist; import javax.swing.boxlayout; import javax.swing.jbutton; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.jtogglebutton; import javax.swing.jtoolbar; import javax.swing.listselectionmodel; import javax.swing.timer; import javax.swing.table.defaulttablemodel; import com.oyp.ftp.ftpclientframe; import com.oyp.ftp.utils.ftpclient; import com.oyp.ftp.utils.ftpfile; /** * 任務隊列控制面板 */ public class queuepanel extends jpanel implements actionlistener { private jtable queuetable = new jtable(); // 顯示任務隊列的表格組件 private jscrollpane scrollpane = new jscrollpane(); private ftpclientframe frame; // 主窗體的引用對象 private string[] columns; private ftpclient ftpclient; // ftp協議的控制類 private timer queuetimer; // 隊列的定時器 private linkedlist<object[]> localqueue; // 本地面板的上傳隊列 private linkedlist<object[]> ftpqueue; // ftp面板的下載隊列 private jtogglebutton stopbutton; private boolean stop = false; // 隊列的控制變量 /** * 默認的構造方法 */ public queuepanel() { initcomponent(); } /** * 自定義的構造方法 * * @param frame * 主窗體 */ public queuepanel(ftpclientframe frame) { this.frame = frame; // 從主窗體獲取本地面板的上傳隊列 localqueue = (linkedlist<object[]>) frame.getlocalpanel().getqueue(); // 從主窗體獲取ftp面板的下載隊列 ftpqueue = (linkedlist<object[]>) frame.getftppanel().getqueue(); initcomponent(); // 初始化窗體界面 // 創建定時器,每間隔1秒執行隊列刷新任務 queuetimer = new timer(1000, new actionlistener() { /** * 定時器的事件處理方法 */ @override public void actionperformed(actionevent e) { if (localqueue.size() + ftpqueue.size() == queuetable .getrowcount()) // 如果隊列大小沒有改變 return; // 結束本方法,不做任何操作 refreshqueue(); // 否則刷新顯示隊列信息的表格數據 } }); } private void initcomponent() { borderlayout cardlayout = new borderlayout(); setlayout(cardlayout); columns = new string[] { "任務名稱", "方向", "主機", "執行狀態" }; queuetable.setmodel(new defaulttablemodel(new object[][] {}, columns)); queuetable.gettableheader().setreorderingallowed(false); scrollpane.setviewportview(queuetable); cardlayout.layoutcontainer(scrollpane); add(scrollpane, center); jtoolbar controltool = new jtoolbar(jtoolbar.vertical); controltool.setrollover(true); controltool.setfloatable(false); jbutton upbutton = new jbutton("上移"); upbutton.setactioncommand("up"); upbutton.addactionlistener(this); jbutton downbutton = new jbutton("下移"); downbutton.setactioncommand("down"); downbutton.addactionlistener(this); stopbutton = new jtogglebutton("暫停"); stopbutton.setactioncommand("stop&start"); stopbutton.addactionlistener(this); jbutton delbutton = new jbutton("刪除"); delbutton.setactioncommand("del"); delbutton.addactionlistener(this); jbutton clearbutton = new jbutton("清空"); clearbutton.setactioncommand("clear"); clearbutton.addactionlistener(this); controltool.setlayout(new boxlayout(controltool, boxlayout.y_axis)); controltool.add(upbutton); controltool.add(downbutton); controltool.add(stopbutton); controltool.add(delbutton); controltool.add(clearbutton); add(controltool, east); } public void startqueue() { ftpclient = frame.getftpclient(); queuetimer.start(); } /** * 界面上按鈕的事件處理方法 */ @override public void actionperformed(actionevent e) { final string command = e.getactioncommand(); if (command.equals("stop&start")) {// 處理暫停按鈕事件 if (stopbutton.isselected()) { stop = true; stopbutton.settext("繼續"); } else { stop = false; stopbutton.settext("暫停"); } } // 處理上移和下移按鈕事件 if (command.equals("up") || command.equals("down")) { up_down_action(command); // 調用處理上移和下移動作的方法 } if (command.equals("del")) {// 處理刪除按鈕的事件 int row = queuetable.getselectedrow(); // 獲取顯示隊列的表格的當前選擇行 if (row < 0) return; // 獲取選擇行的第一個表格單元值 object valueat = queuetable.getvalueat(row, 0); // 如果選擇內容是file類的對象 if (valueat instanceof file) { file file = (file) valueat; int size = localqueue.size(); // 獲取上傳隊列大小 for (int i = 0; i < size; i++) { // 遍歷上傳隊列 if (localqueue.get(i)[0].equals(file)) { localqueue.remove(i); // 從上傳隊列中刪除文件對象 } } } else if (valueat instanceof string) { // 如果選擇的是字符串對象 string filestr = (string) valueat; int size = ftpqueue.size(); // 獲取上傳隊列的大小 for (int i = 0; i < size; i++) { // 遍歷上傳隊列 // 獲取上傳隊列中的文件對象 ftpfile ftpfile = (ftpfile) ftpqueue.get(i)[0]; if (ftpfile.getabsolutepath().equals(filestr)) { ftpqueue.remove(i); // 從上傳隊列中刪除該文件對象 } } } refreshqueue(); // 刷新隊列列表 } if (command.equals("clear")) { // 處理清空按鈕的事件 localqueue.clear(); // 調用本地面板的隊列的clear()方法 ftpqueue.clear(); // 調用ftp面板的隊列的clear()方法 } } /** * 隊列任務的上移和下移動作處理方法 * * @param command * 上移或下移命令 */ private void up_down_action(final string command) { int row = queuetable.getselectedrow(); // 獲取隊列表格的當前選擇行號 if (row < 0) return; // 獲取當前選擇行的第一個單元值 object valueat = queuetable.getvalueat(row, 0); // 獲取當前選擇行的第二個單元值作為判斷上傳或下載方向的依據 string orientation = (string) queuetable.getvalueat(row, 1); if (orientation.equals("上傳")) { // 如果是上傳任務 string value = ((file) valueat).getabsolutepath(); int size = localqueue.size(); for (int i = 0; i < size; i++) { // 遍歷上傳隊列 object[] que = localqueue.get(i); file file = (file) que[0]; // 從隊列中,遍歷到選擇的上傳任務的文件對象 if (file.getabsolutepath().equals(value)) { listselectionmodel selmodel = queuetable .getselectionmodel(); // 獲取表格的選擇模型 selmodel // 設置選擇模型的單選模式 .setselectionmode(listselectionmodel.single_selection); int dsize = localqueue.size(); // 獲取本地上傳隊列的大小 int drow = queuetable.getselectedrow();// 獲取隊列表格的當前選擇行號 int queueval = localqueue.size() - dsize; int next = -1; int selindex = -1; if (command.equals("up")) { if (i < 1) // 限制選擇范圍 return; selindex = drow - queueval - 1; next = i - 1; } else { if (i >= size - 1) // 限制選擇范圍 return; selindex = drow - queueval + 1; next = i + 1; } // 交換連個隊列位置的任務 object[] temp = localqueue.get(next); localqueue.set(next, que); localqueue.set(i, temp); refreshqueue(); // 刷新隊列表格的列表 // 設置表格選擇第一行 selmodel.setselectioninterval(0, selindex); break; } } } else if (orientation.equals("下載")) { // 如果是下載任務 string value = (string) valueat; int size = ftpqueue.size(); // 獲取ftp下載隊列的大小 for (int i = 0; i < size; i++) { // 遍歷下載隊列 object[] que = ftpqueue.get(i); ftpfile file = (ftpfile) que[0]; // 獲取每個下載任務的ftp文件對象 if (file.getabsolutepath().equals(value)) {// listselectionmodel selmodel = queuetable .getselectionmodel(); // 獲取任務隊列表格的選擇模型 // 設置模型使用單選模式 selmodel.setselectionmode(single_selection); int dsize = ftpqueue.size(); int drow = queuetable.getselectedrow(); int queueval = ftpqueue.size() - dsize; int next = -1; int selindex = -1; if (command.equals("up")) { if (i < 1) // 限制選擇范圍 return; selindex = drow - queueval - 1; next = i - 1; } else { if (i >= size - 1) // 限制選擇范圍 return; selindex = drow - queueval + 1; next = i + 1; } // 交換連個隊列位置的任務內容 object[] temp = ftpqueue.get(next); ftpqueue.set(next, que); ftpqueue.set(i, temp); refreshqueue(); // 刷新任務隊列的表格列表 // 選擇表格的第一行 selmodel.setselectioninterval(0, selindex); break; } } } } /** * 刷新隊列的方法 */ private synchronized void refreshqueue() { // 如果自動關機按鈕被按下并且上傳和下載的隊列都有任務 if (frame.getshutdownbutton().isselected() && localqueue.isempty() && ftpqueue.isempty()) { try { // 執行系統關機命令,延遲30秒鐘 runtime.getruntime().exec("shutdown -s -t 30"); } catch (ioexception e) { e.printstacktrace(); } } // 創建表格的數據模型對象 defaulttablemodel model = new defaulttablemodel(columns, 0); // 獲取本地上傳隊列中的任務 object[] localqueuearray = localqueue.toarray(); // 遍歷本地上傳任務 for (int i = 0; i < localqueuearray.length; i++) { object[] queuevalue = (object[]) localqueuearray[i]; if (queuevalue == null) continue; file localfile = (file) queuevalue[0]; // 把上傳隊列的任務添加到表格組件的數據模型中 model.addrow(new object[] { localfile.getabsolutefile(), "上傳", ftpclient.getserver(), i == 0 ? "正在上傳" : "等待上傳" }); } // 獲取下載隊列的任務 object[] ftpqueuearray = ftpqueue.toarray(); // 遍歷下載隊列 for (int i = 0; i < ftpqueuearray.length; i++) { object[] queuevalue = (object[]) ftpqueuearray[i]; if (queuevalue == null) continue; ftpfile ftpfile = (ftpfile) queuevalue[0]; // 把下載隊列的任務添加到表格組件的數據模型中 model.addrow(new object[] { ftpfile.getabsolutepath(), "下載", ftpclient.getserver(), i == 0 ? "正在下載" : "等待下載" }); } queuetable.setmodel(model); // 設置表格使用本方法的表格數據模型 } public boolean isstop() { return stop; } }
5.上傳隊列窗口的實現
package com.oyp.ftp.panel.queue; import java.awt.cardlayout; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.swingutilities; import javax.swing.table.defaulttablemodel; import javax.swing.table.tablecolumn; import com.oyp.ftp.panel.queuetablecellranderer; import com.oyp.ftp.utils.progressarg; public class uploadpanel extends jpanel { private jtable uploadtable = new jtable(); // 表格組件 private jscrollpane scrollpane = new jscrollpane(); private defaulttablemodel model; // 表格的數據模型 /** * 構造方法,用于初始化程序界面 */ public uploadpanel() { cardlayout cardlayout = new cardlayout(); setlayout(cardlayout); progressarg progressarg = new progressarg(-1, -1, -1); model = new defaulttablemodel(new object[][] { new object[] { "", "", "", "", progressarg } }, new string[] { "文件名", "大小", "遠程文件名", "主機", "狀態" }); uploadtable.setmodel(model); uploadtable.gettableheader().setreorderingallowed(false); uploadtable.setrowselectionallowed(false); tablecolumn column = uploadtable.getcolumn("狀態"); column.setcellrenderer(new queuetablecellranderer()); scrollpane.setviewportview(uploadtable); cardlayout.layoutcontainer(scrollpane); add(scrollpane, "queue"); } /** * 向上傳隊列的表格組件添加新任務的方法 * * @param values * - 添加到表格的一行數據的數組對象 */ public void addrow(final object[] values) { runnable runnable = new runnable() { public void run() { model.insertrow(0, values); // 向表格的數據模型添加數據 } }; if (swingutilities.iseventdispatchthread()) runnable.run(); // 在事件隊列執行 else swingutilities.invokelater(runnable); // 或有事件隊列調用 } }
6.下載隊列窗口的實現
package com.oyp.ftp.panel.queue; import java.awt.cardlayout; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.swingutilities; import javax.swing.table.defaulttablemodel; import javax.swing.table.tablecolumn; import com.oyp.ftp.panel.queuetablecellranderer; import com.oyp.ftp.utils.progressarg; public class downloadpanel extends jpanel { private jtable downloadtable = new jtable(); private jscrollpane scrollpane = new jscrollpane(); private defaulttablemodel model; public downloadpanel() { cardlayout cardlayout = new cardlayout(); setlayout(cardlayout); progressarg progressarg = new progressarg(-1, -1, -1); model = new defaulttablemodel(new object[][] { new object[] { "", "", "", "", progressarg } }, new string[] { "文件名", "大小", "本地文件名", "主機", "狀態" }); downloadtable.setmodel(model); downloadtable.gettableheader().setreorderingallowed(false); downloadtable.setrowselectionallowed(false); tablecolumn column = downloadtable.getcolumn("狀態"); column.setcellrenderer(new queuetablecellranderer()); scrollpane.setviewportview(downloadtable); cardlayout.layoutcontainer(scrollpane); add(scrollpane, "queue"); } public void addrow(final object[] values) { runnable runnable = new runnable() { public void run() { model.insertrow(0, values); } }; if (swingutilities.iseventdispatchthread()) runnable.run(); else swingutilities.invokelater(runnable); } }
下面窗口具體的上傳下載后的變化
1.上傳任務的添加
2.上傳任務的完成
3.下載任務的添加
4.下載任務的完成
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/ouyang_peng/article/details/9770325