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

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

3、遠(yuǎn)程窗口主要實(shí)現(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, "遠(yuǎn)程",
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);
//設(shè)置渲染本地資源和FTP資源表格組件的渲染器
ftpDiskTable.getColumnModel().getColumn(0).setCellRenderer(
FTPTableCellRanderer.getCellRanderer());
//RowSorter 的一個(gè)實(shí)現(xiàn),它使用 TableModel 提供排序和過(guò)濾操作。
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
ftpDiskTable.getModel());
TableStringConverter converter = new TableConverter();
//設(shè)置負(fù)責(zé)將值從模型轉(zhuǎn)換為字符串的對(duì)象。
sorter.setStringConverter(converter);
//設(shè)置 RowSorter。RowSorter 用于提供對(duì) JTable 的排序和過(guò)濾。
ftpDiskTable.setRowSorter(sorter);
/**
* 顛倒指定列的排序順序。調(diào)用此方法時(shí),由子類提供具體行為。
* 通常,如果指定列已經(jīng)是主要排序列,則此方法將升序變?yōu)榻敌颍ɑ驅(qū)⒔敌蜃優(yōu)樯颍?
* 否則,使指定列成為主要排序列,并使用升序排序順序。如果指定列不可排序,則此方法沒(méi)有任何效果。
*/
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) { //雙擊鼠標(biāo)
if (selFile.isDirectory()) {
try {
ftpClient.cd(selFile.getAbsolutePath());
listFtpFiles(ftpClient.list());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
/**
* 讀取FTP文件到表格的方法
* @param list
* 讀取FTP服務(wù)器資源列表的輸入流
*/
public synchronized void listFtpFiles(final TelnetInputStream list) {
// 獲取表格的數(shù)據(jù)模型
final DefaultTableModel model = (DefaultTableModel) ftpDiskTable
.getModel();
model.setRowCount(0);
// 創(chuàng)建一個(gè)線程類
Runnable runnable = new Runnable() {
public synchronized void run() {
ftpDiskTable.clearSelection();
try {
String pwd = getPwd(); // 獲取FTP服務(wù)器的當(dāng)前文件夾
model.addRow(new Object[] { new FtpFile(".", pwd, true),
"", "" }); // 添加“.”符號(hào)
model.addRow(new Object[] { new FtpFile("..", pwd, true),
"", "" }); // 添加“..”符號(hào)
/*
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ù)中開(kāi)始做坐標(biāo)為j,i-j為文件名的長(zhǎng)度,文件名在數(shù)據(jù)中的結(jié)束下標(biāo)為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;
}
*/
/* 其中格式應(yīng)滿足如下格式的字符串 結(jié)果為:
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客戶端的設(shè)計(jì)與實(shí)現(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實(shí)習(xí)生招聘TST推薦模板.xls
*<br>d 表示目錄
* <br>- 表示文件
* <br>rw-rw-rw- 表示權(quán)限設(shè)置
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,二進(jìn)制模式為13
// if (names[i]==10) {
if (names[i]==13) {
//獲取字符串 -rwx------ 1 user group 57344 Apr 18 05:32 騰訊電商2013實(shí)習(xí)生招聘TST推薦模板.xls
//文件名在數(shù)據(jù)中開(kāi)始做坐標(biāo)為j,i-j為文件名的長(zhǎng)度,文件名在數(shù)據(jù)中的結(jié)束下標(biāo)為i-1
String fileMessage = new String(names,j,i-j);
if(fileMessage.length() == 0){
System.out.println("fileMessage.length() == 0");
break;
}
//按照空格將fileMessage截為數(shù)組后獲取相關(guān)信息
// 正則表達(dá)式 \s表示空格,{1,}表示1一個(gè)以上
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文件對(duì)象中
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;//上一次位置為二進(jìn)制模式
}
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文件對(duì)象
FtpFile ftpFile = new FtpFile();
// 獲取FTP服務(wù)器目錄信息
String dateStr = data.substring(39, 51).trim();
String sizeOrDir = data.substring(23, 38).trim();
String fileName = data.substring(52, data.length())
.trim();
// 將FTP目錄信息初始化到FTP文件對(duì)象中
ftpFile.setLastDate(dateStr);
ftpFile.setSize(sizeOrDir);
ftpFile.setName(fileName);
ftpFile.setPath(pwd);
// 將文件信息添加到表格中
model.addRow(new Object[] { ftpFile, ftpFile.getSize(),
dateStr });
}
br.close(); // 關(guān)閉輸入流
**********************************************************************/
} catch (IOException ex) {
Logger.getLogger(FTPClientFrame.class.getName()).log(
Level.SEVERE, null, ex);
}
}
};
if (SwingUtilities.isEventDispatchThread()) // 啟動(dòng)線程對(duì)象
runnable.run();
else
SwingUtilities.invokeLater(runnable);
}
/**
* 設(shè)置FTP連接,并啟動(dòng)下載隊(duì)列線程的方法
*/
public void setFtpClient(FtpClient ftpClient) {
this.ftpClient = ftpClient;
// 以30秒為間隔與服務(wù)器保持通訊
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資源管理面板的當(dāng)前文件夾
*/
public void refreshCurrentFolder() {
try {
// 獲取服務(wù)器文件列表
TelnetInputStream list = ftpClient.list();
listFtpFiles(list); // 調(diào)用解析方法
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 開(kāi)始下載隊(duì)列線程
*/
private void startDownThread() {
if (thread != null)
thread.stopThread();
thread = new DownThread(this);
thread.start();
}
/**
* 停止下載隊(duì)列線程
*/
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);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iis創(chuàng)建用戶隔離模式FTP站點(diǎn)的方法
- 如何編寫(xiě)一個(gè)創(chuàng)建FTP站點(diǎn)的函數(shù)?
- Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件 FTP上傳下載管理模塊實(shí)現(xiàn)(11)
- Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件 FTP遠(yuǎn)程文件管理模塊實(shí)現(xiàn)(10)
- Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件 FTP本地文件管理模塊實(shí)現(xiàn)(9)
- Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件 FTP連接管理模塊實(shí)現(xiàn)(8)
- Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件 FTP軟件主界面(4)
- Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件 FTP軟件效果圖預(yù)覽之下載功能(2)
- Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件 FTP協(xié)議分析(1)
- Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件 輔助功能模塊FTP站點(diǎn)管理實(shí)現(xiàn)(12)
相關(guān)文章
解決springboot application.properties server.port配置問(wèn)題
這篇文章主要介紹了解決springboot application.properties server.port配置問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
用Java將字符串的首字母轉(zhuǎn)換大小寫(xiě)
在項(xiàng)目開(kāi)發(fā)的時(shí)候會(huì)需要統(tǒng)一字符串的格式,比如首字母要求統(tǒng)一大寫(xiě)或小寫(xiě),那用Java如何實(shí)現(xiàn)這一功能?下面一起來(lái)學(xué)習(xí)學(xué)習(xí)。2016-08-08
SpringBoot JdbcTemplate批量操作的示例代碼
本篇文章主要介紹了SpringBoot JdbcTemplate批量操作的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Spring?Cloud?整合?nacos實(shí)現(xiàn)動(dòng)態(tài)配置中心的詳細(xì)步驟
這篇文章主要介紹了Spring?Cloud?整合?nacos?實(shí)現(xiàn)動(dòng)態(tài)配置中心,整合步驟是通過(guò)添加依賴新建nacos配置,本文分步驟通過(guò)實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10
Java中數(shù)組和List的互相轉(zhuǎn)換問(wèn)題小結(jié)
這篇文章主要介紹了Java中數(shù)組和List的互相轉(zhuǎn)換問(wèn)題小結(jié),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-03-03
解決創(chuàng)建springboot后啟動(dòng)報(bào)錯(cuò):Failed?to?bind?properties?under‘spri
在Spring?Boot項(xiàng)目中,application.properties和application.yml是用于配置參數(shù)的兩種文件格式,properties格式簡(jiǎn)潔但不支持層次結(jié)構(gòu),而yml格式支持層次性,可讀性更好,在yml文件中,要注意細(xì)節(jié),比如冒號(hào)后面需要空格2024-10-10
Java動(dòng)態(tài)代理之?dāng)r截器的應(yīng)用
今天小編就為大家分享一篇關(guān)于Java動(dòng)態(tài)代理之?dāng)r截器的應(yīng)用,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01

