Java語言實現(xiàn)簡單FTP軟件 FTP軟件遠程窗口實現(xiàn)(6)
本文為大家介紹了FTP軟件遠程窗口的實現(xiàn)方法,供大家參考,具體內(nèi)容如下
1、首先看一下遠程窗口的布局效果

2、看一下本地窗口實現(xiàn)的代碼框架

3、遠程窗口主要實現(xiàn)代碼FtpPanel.java
package com.oyp.ftp.panel.ftp;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ActionMap;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.table.TableStringConverter;
import sun.net.TelnetInputStream;
import com.oyp.ftp.FTPClientFrame;
import com.oyp.ftp.panel.FTPTableCellRanderer;
import com.oyp.ftp.utils.FtpClient;
import com.oyp.ftp.utils.FtpFile;
public class FtpPanel extends javax.swing.JPanel {
FtpClient ftpClient;
private javax.swing.JButton createFolderButton;
private javax.swing.JButton delButton;
private javax.swing.JButton downButton;
javax.swing.JTable ftpDiskTable;
private javax.swing.JLabel ftpSelFilePathLabel;
private javax.swing.JScrollPane scrollPane;
private javax.swing.JToolBar toolBar;
private javax.swing.JButton refreshButton;
private javax.swing.JButton renameButton;
FTPClientFrame frame = null;
Queue<Object[]> queue = new LinkedList<Object[]>();
private DownThread thread;
public FtpPanel() {
initComponents();
}
public FtpPanel(FTPClientFrame client_Frame) {
frame = client_Frame;
initComponents();
}
private void initComponents() {
ActionMap actionMap = getActionMap();
actionMap.put("createFolderAction", new CreateFolderAction(this,
"創(chuàng)建文件夾", null));
actionMap.put("delAction", new DelFileAction(this, "刪除", null));
actionMap.put("refreshAction", new RefreshAction(this, "刷新", null));
actionMap.put("renameAction", new RenameAction(this, "重命名", null));
actionMap.put("downAction", new DownAction(this, "下載", null));
java.awt.GridBagConstraints gridBagConstraints;
toolBar = new javax.swing.JToolBar();
delButton = new javax.swing.JButton();
renameButton = new javax.swing.JButton();
createFolderButton = new javax.swing.JButton();
downButton = new javax.swing.JButton();
refreshButton = new javax.swing.JButton();
scrollPane = new JScrollPane();
ftpDiskTable = new JTable();
ftpDiskTable.setDragEnabled(true);
ftpSelFilePathLabel = new javax.swing.JLabel();
setBorder(javax.swing.BorderFactory.createTitledBorder(null, "遠程",
javax.swing.border.TitledBorder.CENTER,
javax.swing.border.TitledBorder.ABOVE_TOP));
setLayout(new java.awt.GridBagLayout());
toolBar.setRollover(true);
toolBar.setFloatable(false);
delButton.setText("刪除");
delButton.setFocusable(false);
delButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
delButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
delButton.setAction(actionMap.get("delAction"));
toolBar.add(delButton);
renameButton.setText("重命名");
renameButton.setFocusable(false);
renameButton.setAction(actionMap.get("renameAction"));
toolBar.add(renameButton);
createFolderButton.setText("新文件夾");
createFolderButton.setFocusable(false);
createFolderButton.setAction(actionMap.get("createFolderAction"));
toolBar.add(createFolderButton);
downButton.setText("下載");
downButton.setFocusable(false);
downButton.setAction(actionMap.get("downAction"));
toolBar.add(downButton);
refreshButton.setText("刷新");
refreshButton.setFocusable(false);
refreshButton.setAction(actionMap.get("refreshAction"));
toolBar.add(refreshButton);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
add(toolBar, gridBagConstraints);
ftpDiskTable.setModel(new FtpTableModel());
ftpDiskTable.setShowHorizontalLines(false);
ftpDiskTable.setShowVerticalLines(false);
ftpDiskTable.getTableHeader().setReorderingAllowed(false);
ftpDiskTable.setDoubleBuffered(true);
ftpDiskTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
ftpDiskTableMouseClicked(evt);
}
});
scrollPane.setViewportView(ftpDiskTable);
scrollPane.getViewport().setBackground(Color.WHITE);
//設置渲染本地資源和FTP資源表格組件的渲染器
ftpDiskTable.getColumnModel().getColumn(0).setCellRenderer(
FTPTableCellRanderer.getCellRanderer());
//RowSorter 的一個實現(xiàn),它使用 TableModel 提供排序和過濾操作。
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
ftpDiskTable.getModel());
TableStringConverter converter = new TableConverter();
//設置負責將值從模型轉(zhuǎn)換為字符串的對象。
sorter.setStringConverter(converter);
//設置 RowSorter。RowSorter 用于提供對 JTable 的排序和過濾。
ftpDiskTable.setRowSorter(sorter);
/**
* 顛倒指定列的排序順序。調(diào)用此方法時,由子類提供具體行為。
* 通常,如果指定列已經(jīng)是主要排序列,則此方法將升序變?yōu)榻敌颍ɑ驅(qū)⒔敌蜃優(yōu)樯颍?
* 否則,使指定列成為主要排序列,并使用升序排序順序。如果指定列不可排序,則此方法沒有任何效果。
*/
sorter.toggleSortOrder(0);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
add(scrollPane, gridBagConstraints);
ftpSelFilePathLabel.setBorder(javax.swing.BorderFactory
.createEtchedBorder());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
add(ftpSelFilePathLabel, gridBagConstraints);
}
/**
* 表格單擊或雙擊事件的處理方法。
*/
private void ftpDiskTableMouseClicked(java.awt.event.MouseEvent evt) {
int selectedRow = ftpDiskTable.getSelectedRow();
Object value = ftpDiskTable.getValueAt(selectedRow, 0);
if (value instanceof FtpFile) {
FtpFile selFile = (FtpFile) value;
ftpSelFilePathLabel.setText(selFile.getAbsolutePath());
if (evt.getClickCount() >= 2) { //雙擊鼠標
if (selFile.isDirectory()) {
try {
ftpClient.cd(selFile.getAbsolutePath());
listFtpFiles(ftpClient.list());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
/**
* 讀取FTP文件到表格的方法
* @param list
* 讀取FTP服務器資源列表的輸入流
*/
public synchronized void listFtpFiles(final TelnetInputStream list) {
// 獲取表格的數(shù)據(jù)模型
final DefaultTableModel model = (DefaultTableModel) ftpDiskTable
.getModel();
model.setRowCount(0);
// 創(chuàng)建一個線程類
Runnable runnable = new Runnable() {
public synchronized void run() {
ftpDiskTable.clearSelection();
try {
String pwd = getPwd(); // 獲取FTP服務器的當前文件夾
model.addRow(new Object[] { new FtpFile(".", pwd, true),
"", "" }); // 添加“.”符號
model.addRow(new Object[] { new FtpFile("..", pwd, true),
"", "" }); // 添加“..”符號
/*
byte[]names=new byte[2048];
int bufsize=0;
bufsize=list.read(names, 0, names.length);
// list.close();
int i=0,j=0;
while(i<bufsize){
char bc=(char)names[i];
System.out.print(i+" "+bc+" ");
//文件名在數(shù)據(jù)中開始做坐標為j,i-j為文件名的長度,文件名在數(shù)據(jù)中的結束下標為i-1
if (names[i]==13) {
// System.out.println("j:"+j+" i:"+i+ " i-j:"+(i-j));
String temName=new String(names,j,i-j);
System.out.println("temName="+temName);
j=i+2;
}
i=i+1;
}
*/
/* 其中格式應滿足如下格式的字符串 結果為:
0 -: 1 r: 2 w: 3 x: 4 -: 5 -: 6 -: 7 -: 8 -: 9 -: 10 : 11 1: 12 : 13 u: 14 s: 15 e: 16 r: 17 : 18 g: 19 r: 20 o: 21 u: 22 p: 23 : 24 : 25 : 26 : 27 : 28 : 29 : 30 : 31 : 32 6: 33 7: 34 8: 35 4: 36 3: 37 0: 38 : 39 A: 40 p: 41 r: 42 : 43 1: 44 6: 45 : 46 2: 47 1: 48 :: 49 4: 50 6: 51 : 52 F: 53 T: 54 P: 55 ?: 56 ?: 57 ?: 58 ?: 59 ?: 60 ?: 61 ?: 62 ?: 63 ?: 64 ?: 65 ?: 66 ?: 67 ?: 68 ?: 69 ?: 70 ?: 71 ?: 72 ?: 73 .: 74 p: 75 d: 76 f: 77
-rwx------ 1 user group 678430 Apr 16 21:46 FTP客戶端的設計與實現(xiàn).pdf
-rwx------ 1 user group 87504927 Apr 18 22:50 VC.深入詳解(孫鑫)[www.xuexi111.com].pdf
-rwx------ 1 user group 57344 Apr 18 05:32 騰訊電商2013實習生招聘TST推薦模板.xls
*<br>d 表示目錄
* <br>- 表示文件
* <br>rw-rw-rw- 表示權限設置
dateStr:39-51
sizeOrDir:23-38
fileName:52-^
*/
/*********************************************************/
byte[]names=new byte[2048];
int bufsize=0;
bufsize=list.read(names, 0, names.length);
int i=0,j=0;
while(i<bufsize){
//字符模式為10,二進制模式為13
// if (names[i]==10) {
if (names[i]==13) {
//獲取字符串 -rwx------ 1 user group 57344 Apr 18 05:32 騰訊電商2013實習生招聘TST推薦模板.xls
//文件名在數(shù)據(jù)中開始做坐標為j,i-j為文件名的長度,文件名在數(shù)據(jù)中的結束下標為i-1
String fileMessage = new String(names,j,i-j);
if(fileMessage.length() == 0){
System.out.println("fileMessage.length() == 0");
break;
}
//按照空格將fileMessage截為數(shù)組后獲取相關信息
// 正則表達式 \s表示空格,{1,}表示1一個以上
if(!fileMessage.split("\\s+")[8].equals(".") && !fileMessage.split("\\s+")[8].equals("..")){
/**文件大小*/
String sizeOrDir="";
if (fileMessage.startsWith("d")) {//如果是目錄
sizeOrDir="<DIR>";
}else if (fileMessage.startsWith("-")) {//如果是文件
sizeOrDir=fileMessage.split("\\s+")[4];
}
/**文件名*/
String fileName=fileMessage.split("\\s+")[8];
/**文件日期*/
String dateStr =fileMessage.split("\\s+")[5] +" "+fileMessage.split("\\s+")[6]+" " +fileMessage.split("\\s+")[7];
// System.out.println("sizeOrDir="+sizeOrDir);
// System.out.println("fileName="+fileName);
// System.out.println("dateStr="+dateStr);
FtpFile ftpFile = new FtpFile();
// 將FTP目錄信息初始化到FTP文件對象中
ftpFile.setLastDate(dateStr);
ftpFile.setSize(sizeOrDir);
ftpFile.setName(fileName);
ftpFile.setPath(pwd);
// 將文件信息添加到表格中
model.addRow(new Object[] { ftpFile, ftpFile.getSize(),
dateStr });
}
// j=i+1;//上一次位置為字符模式
j=i+2;//上一次位置為二進制模式
}
i=i+1;
}
list.close();
/**********************************************************************
//下面的方法太死了,不夠靈活
BufferedReader br = new BufferedReader(
new InputStreamReader(list)); // 創(chuàng)建字符輸入流
String data = null;
// 讀取輸入流中的文件目錄
while ((data = br.readLine()) != null) {
// 創(chuàng)建FTP文件對象
FtpFile ftpFile = new FtpFile();
// 獲取FTP服務器目錄信息
String dateStr = data.substring(39, 51).trim();
String sizeOrDir = data.substring(23, 38).trim();
String fileName = data.substring(52, data.length())
.trim();
// 將FTP目錄信息初始化到FTP文件對象中
ftpFile.setLastDate(dateStr);
ftpFile.setSize(sizeOrDir);
ftpFile.setName(fileName);
ftpFile.setPath(pwd);
// 將文件信息添加到表格中
model.addRow(new Object[] { ftpFile, ftpFile.getSize(),
dateStr });
}
br.close(); // 關閉輸入流
**********************************************************************/
} catch (IOException ex) {
Logger.getLogger(FTPClientFrame.class.getName()).log(
Level.SEVERE, null, ex);
}
}
};
if (SwingUtilities.isEventDispatchThread()) // 啟動線程對象
runnable.run();
else
SwingUtilities.invokeLater(runnable);
}
/**
* 設置FTP連接,并啟動下載隊列線程的方法
*/
public void setFtpClient(FtpClient ftpClient) {
this.ftpClient = ftpClient;
// 以30秒為間隔與服務器保持通訊
final Timer timer = new Timer(3000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
final FtpClient ftpClient = FtpPanel.this.ftpClient;
if (ftpClient != null && ftpClient.serverIsOpen()) {
ftpClient.noop();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
timer.start();
startDownThread();
}
/**
* 刷新FTP資源管理面板的當前文件夾
*/
public void refreshCurrentFolder() {
try {
// 獲取服務器文件列表
TelnetInputStream list = ftpClient.list();
listFtpFiles(list); // 調(diào)用解析方法
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 開始下載隊列線程
*/
private void startDownThread() {
if (thread != null)
thread.stopThread();
thread = new DownThread(this);
thread.start();
}
/**
* 停止下載隊列線程
*/
public void stopDownThread() {
if (thread != null) {
thread.stopThread();
ftpClient = null;
}
}
public String getPwd() {
String pwd = null;
try {
pwd = ftpClient.pwd();
} catch (IOException e) {
e.printStackTrace();
}
return pwd;
}
public Queue<Object[]> getQueue() {
return queue;
}
/**
* 清除FTP資源表格內(nèi)容的方法
*/
public void clearTable() {
FtpTableModel model = (FtpTableModel) ftpDiskTable.getModel();
model.setRowCount(0);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- iis創(chuàng)建用戶隔離模式FTP站點的方法
- 如何編寫一個創(chuàng)建FTP站點的函數(shù)?
- Java語言實現(xiàn)簡單FTP軟件 FTP上傳下載管理模塊實現(xiàn)(11)
- Java語言實現(xiàn)簡單FTP軟件 FTP遠程文件管理模塊實現(xiàn)(10)
- Java語言實現(xiàn)簡單FTP軟件 FTP本地文件管理模塊實現(xiàn)(9)
- Java語言實現(xiàn)簡單FTP軟件 FTP連接管理模塊實現(xiàn)(8)
- Java語言實現(xiàn)簡單FTP軟件 FTP軟件主界面(4)
- Java語言實現(xiàn)簡單FTP軟件 FTP軟件效果圖預覽之下載功能(2)
- Java語言實現(xiàn)簡單FTP軟件 FTP協(xié)議分析(1)
- Java語言實現(xiàn)簡單FTP軟件 輔助功能模塊FTP站點管理實現(xiàn)(12)
相關文章
解決springboot application.properties server.port配置問題
這篇文章主要介紹了解決springboot application.properties server.port配置問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot JdbcTemplate批量操作的示例代碼
本篇文章主要介紹了SpringBoot JdbcTemplate批量操作的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
Spring?Cloud?整合?nacos實現(xiàn)動態(tài)配置中心的詳細步驟
這篇文章主要介紹了Spring?Cloud?整合?nacos?實現(xiàn)動態(tài)配置中心,整合步驟是通過添加依賴新建nacos配置,本文分步驟通過實例代碼給大家詳細講解,需要的朋友可以參考下2022-10-10
Java中數(shù)組和List的互相轉(zhuǎn)換問題小結
這篇文章主要介紹了Java中數(shù)組和List的互相轉(zhuǎn)換問題小結,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-03-03
解決創(chuàng)建springboot后啟動報錯:Failed?to?bind?properties?under‘spri
在Spring?Boot項目中,application.properties和application.yml是用于配置參數(shù)的兩種文件格式,properties格式簡潔但不支持層次結構,而yml格式支持層次性,可讀性更好,在yml文件中,要注意細節(jié),比如冒號后面需要空格2024-10-10

