<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      在JAVA應用程序中怎么完成FTP的技巧

      [摘要]在JAVA的編程中,您也許會遇到FTP方面的編程,本文就來演示如何實現它。 ---- 本程序是由JBUILDER2.0來開發的,為了節約篇幅我只列出主要的三個部份。FtpList 部分是用來顯示FT...
      在JAVA的編程中,您也許會遇到FTP方面的編程,本文就來演示如何實現它。

      ---- 本程序是由JBUILDER2.0來開發的,為了節約篇幅我只列出主要的三個部份。FtpList 部分是用來顯示FTP服務器上的文件(附圖略)。GetButton部分為從FTP服務器下傳一個文件。PutButton 部分為向FTP服務器上傳一個文件。別忘了在程序中還要引入兩個庫文件(import sun.net.*,import sun.net.ftp.*)。以下是這三部分的JAVA源程序。

      ---- 1)顯示FTP服務器上的文件

      void ftpList_actionPerformed(ActionEvent e) {
      String server=serverEdit.getText();
      //輸入的FTP服務器的IP地址
      String user=userEdit.getText();
      //登錄FTP服務器的用戶名
      String password=passwordEdit.getText();
      //登錄FTP服務器的用戶名的口令
      String path=pathEdit.getText();
      //FTP服務器上的路徑
      try {
      FtpClient ftpClient=new FtpClient();
      //創建FtpClient對象
      ftpClient.openServer(server);
      //連接FTP服務器
      ftpClient.login(user, password);
      //登錄FTP服務器
      if (path.length()!=0) ftpClient.cd(path);
      TelnetInputStream is=ftpClient.list();
      int c;
      while ((c=is.read())!=-1) {
      System.out.print((char) c);}
      is.close();
      ftpClient.closeServer();//退出FTP服務器
      } catch (IOException ex) {;}
      }




      2)從FTP服務器上下傳一個文件


      void getButton_actionPerformed(ActionEvent e) {
      String server=serverEdit.getText();
      String user=userEdit.getText();
      String password=passwordEdit.getText();
      String path=pathEdit.getText();
      String filename=filenameEdit.getText();
      try {
      FtpClient ftpClient=new FtpClient();
      ftpClient.openServer(server);
      ftpClient.login(user, password);
      if (path.length()!=0) ftpClient.cd(path);
      ftpClient.binary();
      TelnetInputStream is=ftpClient.get(filename);
      File file_out=new File(filename);
      FileOutputStream os=new
      FileOutputStream(file_out);
      byte[] bytes=new byte[1024];
      int c;
      while ((c=is.read(bytes))!=-1) {
      os.write(bytes,0,c);
      }
      is.close();
      os.close();
      ftpClient.closeServer();
      } catch (IOException ex) {;}
      }


      3)向FTP服務器上上傳一個文件

      void putButton_actionPerformed(ActionEvent e) {
      String server=serverEdit.getText();
      String user=userEdit.getText();
      String password=passwordEdit.getText();
      String path=pathEdit.getText();
      String filename=filenameEdit.getText();
      try {
      FtpClient ftpClient=new FtpClient();
      ftpClient.openServer(server);
      ftpClient.login(user, password);
      if (path.length()!=0) ftpClient.cd(path);
      ftpClient.binary();
      TelnetOutputStream os=ftpClient.put(filename);
      File file_in=new File(filename);
      FileInputStream is=new FileInputStream(file_in);
      byte[] bytes=new byte[1024];
      int c;
      while ((c=is.read(bytes))!=-1){
      os.write(bytes,0,c);}
      is.close();
      os.close();
      ftpClient.closeServer();
      } catch (IOException ex) {;}
      }
      }


      日韩精品一区二区三区高清