📄 ftpframe.java~6~
字号:
package ftpclient;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.beans.*;import java.io.*;import sun.net.TelnetInputStream;import sun.net.ftp.*;import java.lang.Object;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeModel;import javax.swing.JTextField;import javax.swing.JCheckBox;import javax.swing.tree.TreePath;import sun.net.TelnetOutputStream;import java.util.Properties;/** * <p>Title: FTP上传工具</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2007</p> * <p>Company: 团队合作工作室</p> * @author 沈永维&刘晓鹏 * @version 1.0 */public class FTPFrame extends JFrame { //对象说明 private JPanel contentPane; private JTextField jTextField1 = new JTextField(); private JTextField jTextField2 = new JTextField(); private JCheckBox jCheckbox1 = new JCheckBox(); FtpClient ftp=null; JTabbedPane jTabbedPane1 = new JTabbedPane(); 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 = new JLabel(); 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(); JButton jButton4 = new JButton(); JScrollPane jScrollPane2 = new JScrollPane(); //定义树节点,模型和树视图 DefaultMutableTreeNode root1 = new DefaultMutableTreeNode("目录中没有文件"); DefaultTreeModel model1 = new DefaultTreeModel(root1); JTree jTree1 = new JTree(model1); JButton jButton5 = new JButton(); JLabel statusLabel = 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(); XYLayout xYLayout4 = new XYLayout(); BorderLayout borderLayout1 = new BorderLayout(); XYLayout xYLayout5 = new XYLayout(); PaneLayout paneLayout2 = new PaneLayout(); JPasswordField jPasswordField1 = new JPasswordField(); //Construct the frame public FTPFrame() { enableEvents(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 = Box.createVerticalBox(); jTextField1.setText("192.168.101.2"); contentPane.setLayout(paneLayout2); this.setSize(new Dimension(487, 462)); this.setTitle("文件传输系统"); jTextField2.setText("anonymous"); jCheckbox1.setText("使用匿名"); jCheckbox1.setSelected(true); jPanel1.setLayout(xYLayout5); jLabel1.setFont(new java.awt.Font("Dialog", 0, 14)); jLabel1.setForeground(Color.red); jLabel1.setText("服务器地址:"); jLabel2.setFont(new java.awt.Font("Dialog", 0, 14)); jLabel2.setForeground(Color.red); jLabel2.setText("用 户 名 :"); jLabel3.setFont(new java.awt.Font("Dialog", 0, 14)); jLabel3.setForeground(Color.red); jLabel3.setText("密 码 :"); jTextField4.setText("21"); jLabel4.setFont(new java.awt.Font("Dialog", 0, 14)); jLabel4.setForeground(Color.red); jLabel4.setText("端 口 号:"); jPanel2.setLayout(borderLayout1); jPanel3.setLayout(xYLayout4); jPanel4.setDebugGraphicsOptions(0); jPanel4.setMinimumSize(new Dimension(160, 18)); jPanel4.setPreferredSize(new Dimension(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() { 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)); jTree1.setToggleClickCount(2); jButton5.setText("选择目录"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton5_actionPerformed(e); } }); statusLabel.setFont(new java.awt.Font("Dialog", 0, 14)); statusLabel.setForeground(SystemColor.textHighlight); statusLabel.setText("没有连接ftp服务器"); list1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { list1_mouseClicked(e); } }); jTextArea1.setText(" "); jButton1.setFont(new java.awt.Font("Dialog", 1, 16)); jButton1.setForeground(Color.blue); jButton1.setText("连接服务器"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton2.setFont(new java.awt.Font("Dialog", 1, 16)); jButton2.setForeground(Color.blue); jButton2.setText("断开服务器连接"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton2_actionPerformed(e); } }); list1.setMultipleMode(true); jLabel5.setText("^^FTP服务器目录列表^^"); jLabel6.setText("^^本地硬盘目录列表^^"); contentPane.setOpaque(true); jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jPasswordField1.setText("shy"); jPanel1.setForeground(Color.blue); jPanel1.add(jLabel1, new XYConstraints(96, 9, -1, 30)); jPanel1.add(jTextField1, new XYConstraints(183, 10, 171, 31)); jPanel1.add(jLabel4, new XYConstraints(96, 49, 66, 29)); jPanel1.add(jTextField4, new XYConstraints(185, 46, 173, 27)); jPanel1.add(statusLabel, new XYConstraints(46, 389, -1, -1)); jPanel1.add(jTextField2, new XYConstraints(187, 133, 174, 27)); jPanel1.add(jCheckbox1, new XYConstraints(109, 88, 96, -1)); jPanel1.add(jPasswordField1, new XYConstraints(189, 183, 169, 25)); jPanel1.add(jLabel2, new XYConstraints(94, 131, 66, 27)); jPanel1.add(jLabel3, new XYConstraints(91, 180, 66, 27)); jPanel1.add(jButton1, new XYConstraints(44, 262, 156, 35)); jPanel1.add(jButton2, new XYConstraints(247, 262, 156, 35)); jPanel2.add(box1, BorderLayout.CENTER); jPanel3.add(jScrollPane2, new XYConstraints(60, 60, 0, 0)); jPanel3.add(jTree1, new XYConstraints(301, 16, 161, 255)); jPanel3.add(jButton4, new XYConstraints(184, 138, 98, -1)); jPanel3.add(jButton5, new XYConstraints(186, 195, 97, -1)); jPanel3.add(jButton3, new XYConstraints(185, 69, 97, -1)); jPanel3.add(jScrollPane1, new XYConstraints(20, 20, 156, 258)); jPanel3.add(jLabel5, new XYConstraints(313, 288, -1, -1)); jPanel3.add(jLabel6, new XYConstraints(34, 290, -1, -1)); jScrollPane1.getViewport().add(list1, null); box1.add(jPanel3, null); box1.add(jPanel4, null); jPanel4.add(jScrollPane3, BorderLayout.CENTER); jScrollPane3.getViewport().add(jTextArea1, null); myInit(); jTabbedPane1.add(jPanel2, "文件处理"); jTabbedPane1.add(jPanel1, "连接FTP服务器"); contentPane.add(jTabbedPane1, new PaneConstraints("jTabbedPane1", "jTabbedPane1", PaneConstraints.ROOT, 1.0f)); }//---------------------------------------------------------------------------// 初始化程序: private void myInit()// 作用:对list1和jTree1进行初始化 private void myInit() { File rootfile= new File("c:\\");//得到根目录文件 if(rootfile.isFile()) rootfile=rootfile.getParentFile();//如果得到的不是目录,则使用他的目录 DefaultMutableTreeNode rootTree2 =new DefaultMutableTreeNode(rootfile.getPath()); setTree(rootfile.getPath(),rootTree2);//遍历目录树 model1.setRoot(rootTree2);//设置模型的根节点 model1.reload();//重新构造树视图 if(ftp!=null) { ReloadList(); jTextArea1.append(ftp.welcomeMsg); } } //---------------------------------------------------------------------------// 浏览程序: private void ReloadList()// 作用:清空目录列表,调用List()方法获取文件列表。 private void ReloadList(){ StringBuffer buf=new StringBuffer(); int ch; //清空目录列表 list1.removeAll(); try { //调用List()方法得到目录表 TelnetInputStream t = ftp.list(); t.setStickyCRLF(true); //分解TelnetInputStream数据流 while ( (ch = t.read()) >= 0) { if (ch == '\n') { //向列表框添加分解得到的目录和文件 list1.add(getDIR(buf.toString())); buf.setLength(0); } else { buf.append( (char) ch); } } //完成后关闭TelnetInputStream数据流 t.close(); } catch (IOException ex) { ex.printStackTrace(); } //刷新列表框的内容 list1.validate(); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } }//---------------------------------------------------------------------------// 浏览程序: public String getDIR(String path)// 作用:分解字符串得到目录和文件名 public String getDIR(String path){ String DIRName; int ch;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -