![java實現(xiàn)FTP源代碼12頁_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-8/21/94b5848c-3e06-46b5-a5d3-767bbeb69fcf/94b5848c-3e06-46b5-a5d3-767bbeb69fcf1.gif)
![java實現(xiàn)FTP源代碼12頁_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-8/21/94b5848c-3e06-46b5-a5d3-767bbeb69fcf/94b5848c-3e06-46b5-a5d3-767bbeb69fcf2.gif)
![java實現(xiàn)FTP源代碼12頁_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-8/21/94b5848c-3e06-46b5-a5d3-767bbeb69fcf/94b5848c-3e06-46b5-a5d3-767bbeb69fcf3.gif)
![java實現(xiàn)FTP源代碼12頁_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-8/21/94b5848c-3e06-46b5-a5d3-767bbeb69fcf/94b5848c-3e06-46b5-a5d3-767bbeb69fcf4.gif)
![java實現(xiàn)FTP源代碼12頁_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-8/21/94b5848c-3e06-46b5-a5d3-767bbeb69fcf/94b5848c-3e06-46b5-a5d3-767bbeb69fcf5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、package ftpclient;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.beans.*;import java.io.*;import .TelnetInputStream;import .ftp.*;import java.lang.Object;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeMod
2、el;import javax.swing.JTextField;import javax.swing.JCheckBox;import javax.swing.tree.TreePath;import .TelnetOutputStream;import java.util.Properties;/* * Title: an example * Description:an no * Copyright: Copyright (c) 2002 * Company: home * author liujun * version 1.0 */public class Frame1 extends
3、 JFrame private JPanel contentPane; private JTextField jTextField1 = new JTextField(); private JTextField jTextField2 = new JTextField(); private JTextField jTextField3 = new JTextField(); private JCheckBox jCheckbox1 = new JCheckBox(); FtpClient ftp=null; JTabbedPane jTabbedPane1 = new JTabbedPane(
4、); JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); PaneLayout paneLayout1 = new PaneLayout(); XYLayout xYLayout1 = new XYLayout(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); JTextField jTextField4 = new JTextField(); JLabel jLabel4 = ne
5、w JLabel(); BorderLayout borderLayout1 = new BorderLayout(); Box box1; JPanel jPanel3 = new JPanel(); JPanel jPanel4 = new JPanel(); XYLayout xYLayout2 = new XYLayout(); BorderLayout borderLayout2 = new BorderLayout(); JScrollPane jScrollPane1 = new JScrollPane(); JButton jButton3 = new JButton(); J
6、Button jButton4 = new JButton(); JScrollPane jScrollPane2 = new JScrollPane(); /定義樹節(jié)點,模型和樹視圖 DefaultMutableTreeNode root2 = new DefaultMutableTreeNode(目錄中沒有文件); DefaultTreeModel model2 = new DefaultTreeModel(root2); JTree jTree2 = new JTree(model2); JButton jButton5 = new JButton(); JLabel statusLab
7、el = new JLabel(); List list1 = new List(); JScrollPane jScrollPane3 = new JScrollPane(); JTextArea jTextArea1 = new JTextArea(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JLabel jLabel5 = new JLabel(); JLabel jLabel6 = new JLabel(); /Construct the frame public Frame1() ena
8、bleEvents(AWTEvent.WINDOW_EVENT_MASK); try jbInit(); catch(Exception e) e.printStackTrace(); /Component initialization private void jbInit() throws Exception /setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource(Your Icon); contentPane = (JPanel) this.getContentPane(); box1
9、= Box.createVerticalBox(); jTextField1.setText(); contentPane.setLayout(paneLayout1); this.setSize(new Dimension(487, 462); this.setTitle(文件傳輸系統(tǒng)); jTextField2.setText(anonymous); jTextField3.setText(a); jCheckbox1.setText(使用匿名); jCheckbox1.setSelected(true); jPanel1.setLayout(xYLayout1)
10、; jLabel1.setText(服務器地址:); jLabel2.setText(用戶名:); jLabel3.setText(密 碼:); jTextField4.setText(21); jLabel4.setText(端口號:); jPanel2.setLayout(borderLayout1); jPanel3.setLayout(xYLayout2); jPanel4.setDebugGraphicsOptions(0); jPanel4.setMinimumSize(new Dimension(160, 18); jPanel4.setPreferredSize(new Dim
11、ension(160, 18); jPanel4.setLayout(borderLayout2); jButton3.setText(文件上載); jButton3.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(ActionEvent e) jButton3_actionPerformed(e); ); jButton4.setText(文件下載); jButton4.addActionListener(new java.awt.event.ActionListener()
12、public void actionPerformed(ActionEvent e) jButton4_actionPerformed(e); ); box1.setEnabled(true); jPanel3.setDebugGraphicsOptions(0); jPanel3.setMinimumSize(new Dimension(400, 240); jPanel3.setPreferredSize(new Dimension(400, 240); jTree2.setToggleClickCount(2); jButton5.setText(選擇目錄); jButton5.addA
13、ctionListener(new java.awt.event.ActionListener() public void actionPerformed(ActionEvent e) jButton5_actionPerformed(e); ); statusLabel.setText(沒有連接ftp服務器); list1.addMouseListener(new java.awt.event.MouseAdapter() public void mouseClicked(MouseEvent e) list1_mouseClicked(e); ); jTextArea1.setText(
14、); jButton1.setText(連接到服務器); jButton1.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(ActionEvent e) jButton1_actionPerformed(e); ); jButton2.setText(斷開服務器連接); jButton2.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(ActionEvent e)
15、jButton2_actionPerformed(e); ); list1.setMultipleMode(true); jLabel5.setText(FTP服務器目錄列表); jLabel6.setText(本地硬盤目錄列表); jPanel1.add(jTextField1, new XYConstraints(81, 11, 297, -1); jPanel1.add(jLabel1, new XYConstraints(21, 11, -1, -1); jPanel1.add(statusLabel, new XYConstraints(-2, 370, 484, 29); jPan
16、el1.add(jButton2, new XYConstraints(34, 304, 122, -1); jPanel1.add(jButton1, new XYConstraints(34, 268, 106, 23); jPanel1.add(jLabel4, new XYConstraints(22, 50, -1, -1); jPanel1.add(jTextField4, new XYConstraints(82, 49, 88, -1); jPanel1.add(jCheckbox1, new XYConstraints(22, 84, -1, -1); jPanel1.add
17、(jTextField2, new XYConstraints(80, 124, 86, -1); jPanel1.add(jLabel2, new XYConstraints(20, 124, 58, 21); jPanel1.add(jTextField3, new XYConstraints(80, 165, 87, -1); jPanel1.add(jLabel3, new XYConstraints(20, 166, -1, -1); jPanel2.add(box1, BorderLayout.CENTER); jPanel3.add(jScrollPane2, new XYCon
18、straints(295, 6, 173, 280); jPanel3.add(jScrollPane1, new XYConstraints(7, 6, 194, 281); jPanel3.add(jButton4, new XYConstraints(205, 93, 84, 19); jPanel3.add(jButton3, new XYConstraints(205, 53, 86, 19); jPanel3.add(jButton5, new XYConstraints(205, 136, 85, 19); jPanel3.add(jLabel5, new XYConstrain
19、ts(16, 293, 144, 22); jPanel3.add(jLabel6, new XYConstraints(314, 293, 144, 22); jScrollPane1.getViewport().add(list1, null); jScrollPane2.getViewport().add(jTree2, null); box1.add(jPanel3, null); box1.add(jPanel4, null); jPanel4.add(jScrollPane3, BorderLayout.CENTER); jScrollPane3.getViewport().add
20、(jTextArea1, null); myInit(); jTabbedPane1.add(jPanel2, 文件處理); jTabbedPane1.add(jPanel1, 連接FTP服務器); contentPane.add(jTabbedPane1, new PaneConstraints(jTabbedPane1, jTabbedPane1, PaneConstraints.ROOT, 0.5f); private void myInit() File rootfile= new File(c:);/得到根目錄文件 if(rootfile.isFile() rootfile=root
21、file.getParentFile();/如果得到的不是目錄,則使用他的目錄 DefaultMutableTreeNode rootTree2 =new DefaultMutableTreeNode(rootfile.getPath(); setTree(rootfile.getPath(),rootTree2);/遍歷目錄樹 model2.setRoot(rootTree2);/設置模型的根節(jié)點 model2.reload();/重新構(gòu)造樹視圖 if(ftp!=null) ReloadList(); jTextArea1.append(ftp.welcomeMsg); /-/ 瀏覽程序:
22、private void ReloadList()/ 作用:清空目錄列表,調(diào)用List()方法獲取文件列表。 private void ReloadList() StringBuffer buf=new StringBuffer(); int ch; /清空目錄列表 list1.removeAll(); try /調(diào)用List()方法得到目錄表 TelnetInputStream t = ftp.list(); t.setStickyCRLF(true); /分解TelnetInputStream數(shù)據(jù)流 while ( (ch = t.read() = 0) if (ch = n) /向列表框
23、添加分解得到的目錄和文件 list1.add(getDIR(buf.toString(); buf.setLength(0); else buf.append( (char) ch); /完成后關(guān)閉TelnetInputStream數(shù)據(jù)流 t.close(); catch (IOException ex) ex.printStackTrace(); /刷新列表框的內(nèi)容 list1.validate(); /Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e
24、) cessWindowEvent(e); if (e.getID() = WindowEvent.WINDOW_CLOSING) System.exit(0); public String getDIR(String path) String DIRName; int ch; /分解字符串得到目錄和文件名 int begin=55; DIRName=path.substring(begin).trim(); return DIRName; /-/ 瀏覽程序: public void setTree(String Path,DefaultMutableTreeNode Tre
25、ePath)/ 作用:構(gòu)造jTree對象的目錄樹 public void setTree(String Path,DefaultMutableTreeNode TreePath) /遞歸遍歷目錄樹 try File source = new File(Path);/得到源文件路徑 String fileName=source.list();/的到該目錄下文件列表 for(int i=0;i0) /選擇列表框中單擊選中的內(nèi)容 String dir=list1.getSelectedItem().trim(); try /如果是目錄,則進入目錄 ftp.cd(dir); /列表框需要重新刷新 Re
26、loadList(); jTextArea1.append(進入目錄: ); jTextArea1.append(dir); jTextArea1.append(n); catch (IOException ex) /否則,表明點擊的內(nèi)容是ftp上的一個文件 jTextArea1.append(dir); jTextArea1.append(:是一個文件n); /-/ 登錄程序: void jButton1_actionPerformed(ActionEvent e)/ 作用:登錄連接到FTP服務器。 void jButton1_actionPerformed(ActionEvent e) i
27、nt ch; String hostname=jTextField1.getText(); /如果已經(jīng)打開了FTP服務器,則先關(guān)閉FTP文件服務器 try if (ftp!=null) ftp.closeServer(); catch (IOException ex) ex.printStackTrace(); /連接到服務器 try statusLabel.setText(正在連接,請等待.); ftp= new FtpClient(hostname); /登錄Ftp服務器 ftp.login(jTextField2.getText(),jTextField3.getText(); /使用二
28、進制協(xié)議 ftp.binary(); catch(FtpLoginException ex) /沒有主機的登錄權(quán)限 statusLabel.setText(無權(quán)限與主機:+hostname+連接!); catch (IOException ex) /連接主機失敗 statusLabel.setText(連接主機:+hostname+失敗!); catch(SecurityException ex) /用戶或者密碼可能不對 statusLabel.setText(用戶或者密碼可能不對,無權(quán)限與主機:+hostname+連接!); /連接成功后的顯示 statusLabel.setText(連接主
29、機:+hostname+成功!); /列表框需要重新刷新 ReloadList(); void jButton2_actionPerformed(ActionEvent e) try /關(guān)閉FTP服務器連接 if (ftp!=null) ftp.closeServer(); list1.removeAll(); catch (IOException ex) ex.printStackTrace(); statusLabel.setText(斷開主機連接成功!); void jButton3_actionPerformed(ActionEvent e) /上載本地文件到ftp服務器中 if(jT
30、ree2.isSelectionEmpty() ) jTextArea1.append(沒有選擇本地文件上載n); else File file1=new File(conPath(); /判斷是否是文件,如果是目錄則不與操作,以后的程序可能會考慮添加目錄的操作 if(file1.isFile() String filename=jTree2.getSelectionPath().getLastPathComponent().toString(); int ch; try File localFile = new File(conPath() ); RandomAccessFile sendF
31、ile = new RandomAccessFile( conPath() , r); /上載文件到ftp服務器中 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 (IOExcepti
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年礦物制品及材料批發(fā)服務合作協(xié)議書
- 建設工程項目聯(lián)合施工協(xié)議書范本
- 二零二五年度藝術(shù)類合作協(xié)議書:藝術(shù)品投資與收藏合作框架
- 二零二五年度自費留學國際志愿者項目合作合同
- 2025年度醫(yī)療事故調(diào)解與糾紛預防合作協(xié)議
- 醫(yī)院合同制人員2025年度工資調(diào)整與職業(yè)成長激勵合同
- 二零二五年度足浴店員工工作績效與獎勵合同
- 人教版地理八年級上冊《第二節(jié) 氣候》聽課評課記錄1
- 二零二五年度酒店住宿消費者返利協(xié)議集
- 2025年度消費者權(quán)益保護糾紛合同范本
- 五年級數(shù)學(小數(shù)乘除法)計算題專項練習及答案匯編
- 上海市楊浦區(qū)2024-2025學年八年級上學期英語期末考卷(含筆試答案無聽力答案、原文及音頻)
- 2024年蘇州農(nóng)業(yè)職業(yè)技術(shù)學院高職單招語文歷年參考題庫含答案解析
- 課題申報參考:法國漢學家弗朗索瓦·朱利安對中國山水畫論的闡釋研究
- 2024年09月2024年中國農(nóng)業(yè)發(fā)展銀行總行部門秋季校園招聘(22人)筆試歷年參考題庫附帶答案詳解
- 2025年北京生命科技研究院招聘筆試參考題庫含答案解析
- 銀行金融機構(gòu)銀行金融服務協(xié)議
- GB/T 27697-2024立式油壓千斤頂
- 《消防機器人相關(guān)技術(shù)研究》
- 游泳館安全隱患排查
- 《媒介社會學》課件
評論
0/150
提交評論