⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpclientfram.java

📁 用java编写的一个FTP客户端的应用程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
       textAreaContent.setBounds(new Rectangle(25,135,420,203));
       textAreaContent.setEditable(false);
       
       labelFWQDir.setText("服务器目录:");
       labelFWQDir.setBounds(new Rectangle(25,348, 100, 22));
       textFieldFWQDir.setBounds(new Rectangle(128,348, 230,22));
       buttonCd.setLabel("进入目录");
       buttonCd.setBounds(new Rectangle(375,348, 70,22));
       buttonCd.addActionListener(new java.awt.event.ActionListener(){
           public void actionPerformed(ActionEvent e){
               buttonCd_actionPerformed(e);
           }
       });
      buttonCd.setEnabled(false);
       
       labelFile.setText("上传/下载文件名:");
       labelFile.setBounds(new Rectangle(25,380,100,22));
       textFieldFile.setBounds(new Rectangle(128,380,230,22));
       labelDir.setText("本地文件路径");
       labelDir.setBounds(new Rectangle(25,412, 100,22));
       textFieldDir.setBounds(new Rectangle(128,412,230,22));
       
       buttonDownload.setLabel("下载");
       buttonDownload.setBounds(new Rectangle(375,412,70,22));
       buttonDownload.addActionListener(new java.awt.event.ActionListener(){
           public void actionPerformed(ActionEvent e){
               buttonDownload_actionPerformed(e);
           }
       });
       buttonDownload.setEnabled(false);
       
       buttonPut.setLabel("上传");
       buttonPut.setBounds(new Rectangle(375,380,70,22));
       buttonPut.addActionListener(new java.awt.event.ActionListener(){
           public void actionPerformed(ActionEvent e){
               buttonPut_actionPerformed(e);
           }
       });
       buttonPut.setEnabled(false);
       
       contentPane.add(labelPrompt,null);
       contentPane.add(labelHost,null);
       contentPane.add(textFieldHost,null);
       contentPane.add(labelUser,null);
       contentPane.add(textFieldUser,null);
       contentPane.add(labelPassword,null);
       contentPane.add(textFieldPassword,null);
       contentPane.add(buttonLink,null);
       contentPane.add(buttonDisconnect,null);
       
       contentPane.add(labelFileShow,null);
       contentPane.add(textAreaContent,null);
       contentPane.add(textFieldFile,null);
       contentPane.add(labelFile,null);
       contentPane.add(labelDir,null);
       contentPane.add(textFieldDir,null);
       contentPane.add(buttonDownload,null);
       contentPane.add(buttonPut,null);
       contentPane.add(labelFWQDir,null);
       contentPane.add(textFieldFWQDir,null);
       contentPane.add(buttonCd,null);
       
       enableEvents(AWTEvent.WINDOW_EVENT_MASK);
       
       this.setSize(new Dimension(480, 485));
       this.setResizable(false);
       this.setTitle("FTP客户端");
       this.setVisible(true);
   }
   
   void buttonLink_actionPerformed(ActionEvent e){
       String hostname = textFieldHost.getText();
       labelPrompt.setText("正在连接,请等待......");
       try {
           this.openConnection(hostname); // 控制连接建立
           this.getMsgs() ; // 启动接收线程
           this.Login();  //登陆服务器
           this.ListFile();     //得到文件列表
           labelPrompt.setText("连接主机:" + textFieldHost.getText() + "成功!");
           buttonDisconnect.setEnabled(true);
           buttonDownload.setEnabled(true);
           buttonPut.setEnabled(true);
           buttonLink.setEnabled(false);
           buttonCd.setEnabled(true);
       }
       catch(IOException e1){
           String strPrompt = "连接主机:" + hostname + "失败!";
           labelPrompt.setText(strPrompt);
       }
       catch(SecurityException e1){
           String strPrompt = "无权限与主机:" + hostname + "连接!";
           labelPrompt.setText(strPrompt);
       }
       catch(Exception e1){
           String strPrompt = "用户名密码错误";
           labelPrompt.setText(strPrompt);
       }
   }
   
   void buttonDisconnect_actionPerformed(ActionEvent e){
       try{
           doQuit();
           textAreaContent.setText("");
           labelPrompt.setText("与主机断开连接");
       }
       catch(Exception e1){
           System.out.println("Error:" + e1);
       }
       buttonLink.setEnabled(true);
       buttonDownload.setEnabled(false);
       buttonPut.setEnabled(false);
       buttonDisconnect.setEnabled(false);
       buttonCd.setEnabled(false);
   }
   
   void buttonCd_actionPerformed(ActionEvent e){
        String dirName = "" ; 
       try{
           dirName = textFieldFWQDir.getText();
           ctrlOutput.println("CWD " + dirName) ;// CWD命令
           ctrlOutput.flush() ;
           textAreaContent.setText("");
           ListFile();
       }catch(Exception e1) {
           e1.printStackTrace();
           System.exit(1);
       }
   }
  
   
   void buttonDownload_actionPerformed(ActionEvent e){
       String fileName = "" ;
       BufferedReader lineread= new BufferedReader(new InputStreamReader(System.in)) ;
       try{
           int n ;
           byte[] buff = new byte[1024] ;
           // 指定服务器上的文件名
           fileName = textFieldFile.getText();
           // 在客户端上准备接收用的文件
           FileOutputStream outfile = new FileOutputStream(fileName) ;
           // 构造传输文件用的数据流
           Socket dataSocket = dataConnection("RETR " + fileName) ;
           BufferedInputStream dataInput= new BufferedInputStream(dataSocket.getInputStream()) ;
           // 接收来自服务器的数据,写入本地文件
           while((n = dataInput.read(buff)) > 0){
               outfile.write(buff,0,n) ;
           }
           dataSocket.close() ;
           outfile.close() ;
           JOptionPane msg = new JOptionPane();
           msg.showMessageDialog(FTPClientFram.this,"下载成功","下载成功!",1);            
       }catch(Exception e1) {
           e1.printStackTrace();
           System.exit(1);
       }
   }
   
   void buttonPut_actionPerformed(ActionEvent e){
       String fileName = "" ;
       try{
           int n ;
           byte[] buff = new byte[1024] ;
           FileInputStream sendfile = null ;           
           // 指定文件名
           fileName = textFieldFile.getText();
           // 准备读出客户端上的文件
           try{
               sendfile = new FileInputStream(fileName) ;
           }catch(Exception e1){
               System.out.println("file not existed") ;
               return ;
           }            
           // 准备发送数据的流
           Socket dataSocket = dataConnection("STOR " + fileName) ;
           OutputStream outstr = dataSocket.getOutputStream() ;
           // 读出文件,并利用网络发送给服务器
           while((n = sendfile.read(buff)) > 0){
               outstr.write(buff,0,n) ;
           }
           dataSocket.close();
           sendfile.close();
           JOptionPane msg = new JOptionPane();
           msg.showMessageDialog(FTPClientFram.this,"上传成功","上传成功!",1);  
           textAreaContent.setText("");
           ListFile();
       }catch(Exception e1) {
           e1.printStackTrace();
           System.exit(1);
       }
   }
   
   protected void processWindowEvent(WindowEvent e){
       super.processWindowEvent(e);
       if(e.getID()==WindowEvent.WINDOW_CLOSING){
           try{
               if(ctrlSocket != null)
                   ctrlSocket.close();
           }
           catch(IOException e1){
               System.out.println("Error:" + e1);
           }
           System.exit(0);
       }
   }
   
   public static void main(String[] args){
       new FTPClientFram();
   }
}

//CtrlListen 类
class CtrlListen implements Runnable{
   BufferedReader ctrlInput = null ;
   // 构造器 指定读取地址
   public CtrlListen(BufferedReader in){
       ctrlInput = in ;
   }
   
   public void run() {
       while(true){
           try{ // 按行读入并输出到标准输出上
               System.out.println(ctrlInput.readLine()) ;
           } catch (Exception e){
               System.exit(1) ;
           }
       }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -