?? frame1.java~215~
字號:
public void setTree(String Path,DefaultMutableTreeNode TreePath){
//遞歸遍歷目錄樹
try{
File source = new File(Path);//得到源文件路徑
String[] fileName=source.list();//的到該目錄下文件列表
for(int i=0;i<fileName.length;i++){//顯示選擇了的文件列表
File fileMem=new File(source.getPath(),fileName[i]);
if(fileMem.isDirectory()){//是目錄則添加目錄
DefaultMutableTreeNode TreeMem =new DefaultMutableTreeNode(fileMem.getName());
TreePath.add(TreeMem);//在樹中添加目錄
setTree(fileMem.getPath(),TreeMem);//遞歸遍歷目錄樹
}//end if
else{//是文件則添加節(jié)點(diǎn)
DefaultMutableTreeNode TreeMem =new DefaultMutableTreeNode(fileMem.getName());
TreePath.add(TreeMem);//添加節(jié)點(diǎn)
}
}//end for
}//End try
catch(Exception ei)
{
JOptionPane.showMessageDialog(this, "系統(tǒng)出錯(cuò):"+ei);//顯示提示信息。
}
}
void JCheckbox1_propertyChange(PropertyChangeEvent e) {
if(jCheckbox1.isSelected()){
jTextField1.setText("anonymous");
jTextField2.setText("a");
}
}
//---------------------------------------------------------------------------
// 瀏覽程序: void jButton5_actionPerformed(ActionEvent e)
// 作用:選擇需要的本地文件目錄
void jButton5_actionPerformed(ActionEvent e) {
JFileChooser JFileCh = new JFileChooser();//創(chuàng)建文件對話框
JFileCh.setFileSelectionMode(JFileCh.DIRECTORIES_ONLY);//只選擇目錄
int returnVal = JFileCh.showOpenDialog(this);//顯示文件對話框
if(returnVal == JFileChooser.APPROVE_OPTION){
File root2 = JFileCh.getSelectedFile();//得到根目錄文件
if(root2.isFile()) root2=root2.getParentFile();//如果得到的不是目錄,則使用他的目錄
DefaultMutableTreeNode rootTree2 =new DefaultMutableTreeNode(root2.getPath());
setTree(root2.getPath(),rootTree2);//遍歷目錄樹
model2.setRoot(rootTree2);//設(shè)置模型的根節(jié)點(diǎn)
model2.reload();//重新構(gòu)造樹視圖
}//end if
else
JOptionPane.showMessageDialog(this, "沒有選擇文件");//顯示提示信息。
}
//---------------------------------------------------------------------------
// 瀏覽程序:void list1_mouseClicked(MouseEvent e)
// 作用:FTP服務(wù)器文件目錄改變
void list1_mouseClicked(MouseEvent e) {
//單擊列表框的操作
StringBuffer buf=new StringBuffer();
if(ftp==null) list1.removeAll();
if(list1.getRows()>0){
//選擇列表框中單擊選中的內(nèi)容
String dir=list1.getSelectedItem().trim();
try {
//如果是目錄,則進(jìn)入目錄
ftp.cd(dir);
//列表框需要重新刷新
ReloadList();
jTextArea1.append("進(jìn)入目錄: ");
jTextArea1.append(dir);
jTextArea1.append("\n");
}
catch (IOException ex) {
//否則,表明點(diǎn)擊的內(nèi)容是ftp上的一個(gè)文件
jTextArea1.append(dir);
jTextArea1.append(":是一個(gè)文件\n");
}
}
}
//---------------------------------------------------------------------------
// 登錄程序: void jButton1_actionPerformed(ActionEvent e)
// 作用:登錄連接到FTP服務(wù)器。
void jButton1_actionPerformed(ActionEvent e) {
int ch;
String hostname=jTextField1.getText();
//如果已經(jīng)打開了FTP服務(wù)器,則先關(guān)閉FTP文件服務(wù)器
try {
if (ftp!=null) ftp.closeServer();
}
catch (IOException ex) {
ex.printStackTrace();
}
//連接到服務(wù)器
try {
statusLabel.setText("正在連接,請等待.....");
ftp= new FtpClient(hostname);
//登錄Ftp服務(wù)器
ftp.login(jTextField2.getText(),jTextField3.getText());
//使用二進(jìn)制協(xié)議
ftp.binary();
}
catch(FtpLoginException ex){
//沒有主機(jī)的登錄權(quán)限
statusLabel.setText("無權(quán)限與主機(jī):"+hostname+"連接!");
}
catch (IOException ex){
//連接主機(jī)失敗
statusLabel.setText("連接主機(jī):"+hostname+"失敗!");
}
catch(SecurityException ex)
{
//用戶或者密碼可能不對
statusLabel.setText("用戶或者密碼可能不對,無權(quán)限與主機(jī):"+hostname+"連接!");
}
//連接成功后的顯示
statusLabel.setText("連接主機(jī):"+hostname+"成功!");
//列表框需要重新刷新
ReloadList();
}
//---------------------------------------------------------------------------
// 關(guān)閉程序: void jButton3_actionPerformed(ActionEvent e)
// 作用:關(guān)閉FTP服務(wù)器連接
void jButton2_actionPerformed(ActionEvent e) {
try {
//關(guān)閉FTP服務(wù)器連接
if (ftp!=null) {
ftp.closeServer();
list1.removeAll();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
statusLabel.setText("斷開主機(jī)連接成功!");
}
//---------------------------------------------------------------------------
// 上載程序: void jButton3_actionPerformed(ActionEvent e)
// 作用:上載本地文件到ftp目錄
void jButton3_actionPerformed(ActionEvent e) {
//上載本地文件到ftp服務(wù)器中
if(jTree2.isSelectionEmpty() ){
jTextArea1.append("沒有選擇本地文件上載\n");
}else{
File file1=new File(conPath());
//判斷是否是文件,如果是目錄則不與操作,以后的程序可能會(huì)考慮添加目錄的操作
if(file1.isFile()){
String filename=jTree2.getSelectionPath().getLastPathComponent().toString();
int ch;
try {
File localFile = new File(conPath() );
RandomAccessFile sendFile = new RandomAccessFile( conPath() , "r");
//上載文件到ftp服務(wù)器中
sendFile.seek(0);
TelnetOutputStream outs = ftp.put(filename);
DataOutputStream outputs = new DataOutputStream(outs);
//上載處理中......
while (sendFile.getFilePointer() < sendFile.length()) {
ch = sendFile.read();
outputs.write(ch);
}
//上載完成,關(guān)閉輸入輸出流
outs.close();
sendFile.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
//列表框需要重新刷新
ReloadList();
}
}
}
//---------------------------------------------------------------------------
// 下載程序: void jButton4_actionPerformed(ActionEvent e)
// 作用:下載ftp文件到本地目錄
void jButton4_actionPerformed(ActionEvent e) {
//下載ftp文件到本地目錄
if(jTree2.isSelectionEmpty()){
//如果沒有選擇目錄和文件提示用戶
jTextArea1.append("沒有選擇本地目錄用于下載!!\n");
}else{
File file1=new File(conPath());
if(file1.isFile()){
//如果是文件也要提示用戶選擇的是文件
jTextArea1.append("沒有選擇本地目錄用于下載"+jTree2.getSelectionPath().getLastPathComponent().toString()+"\n");
jTextArea1.append("而選擇的是文件:"+conPath()+"\n");
}else{
//下載文件到指定的目錄里
jTextArea1.append("選擇的下載本地目錄"+jTree2.getSelectionPath().getLastPathComponent().toString()+"\n");
jTextArea1.append("目錄:"+conPath()+"\n");
//可以選擇多個(gè)文件和目錄
String [] filenames=list1.getSelectedItems();
for(int i=0;i<filenames.length;i++){
String filename=filenames[i];
boolean isfile=false;
try {
//判斷是否為目錄,如果是目錄則在本地創(chuàng)建一個(gè)目錄
ftp.cd(filename);
ftp.cdUp();
File localDir = new File(conPath()+"\\"+filename) ;
localDir.mkdir();
}
catch (IOException ex) {
isfile=true;
}
if(isfile){
//對文件的處理,即下載文件到本地硬盤中
int ch;
try{
File localFile = new File(conPath()+"\\"+filename) ;
RandomAccessFile getFile = new RandomAccessFile((conPath()+"\\"+filename),"rw");
getFile.seek(0);
TelnetInputStream ins = ftp.get(filename);
DataInputStream Inputs = new DataInputStream(ins);
//下載處理......
while ((ch = Inputs.read()) >= 0) {
getFile.write(ch);
}
//處理完成,關(guān)閉輸入輸出流
ins.close();
getFile.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
}
String conPath( ){
//用于獲取本地目錄樹中的文件路徑
TreePath treePath1=jTree2.getSelectionPath();
String dir1,dir=treePath1.getPathComponent(0).toString();
int i,count=treePath1.getPathCount();
//對于象“c:\”之類的盤符路徑需要特殊處理
if((dir.length())==3)
dir1=dir.substring(0,2);
else
dir1=dir;
for(i=1;i<count;i++){
dir1=dir1+"\\"+treePath1.getPathComponent(i);
}
return dir1;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -