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

📄 ftpconnection.java

📁 一个利用Java语言实现的ftp程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
         out.write(mFtpStatus.getResponse(214, tempRequest, mUser, args));
         return;
     } 
     
     
     /**
      * <code>LIST [&lt;SP&gt; &lt;pathname&gt;] &lt;CRLF&gt;</code><br>
      *
      * This command causes a list to be sent from the server to the
      * passive DTP.  If the pathname specifies a directory or other
      * group of files, the server should transfer a list of files
      * in the specified directory.  If the pathname specifies a
      * file then the server should send current information on the
      * file.  A null argument implies the user's current working or
      * default directory.  The data transfer is over the data
      * connection
      */
     public void doLIST(FtpRequest request, FtpWriter out) throws IOException {
         
         Writer os = null;
         try {
            
             // reset state variables
             resetState();
         
             // get data connection
             out.write(mFtpStatus.getResponse(150, request, mUser, null));
             Socket dataSoc = mDataConnection.getDataSocket();
             if (dataSoc == null) {
                  out.write(mFtpStatus.getResponse(550, request, mUser, null));
                  return;
             }
             
             // transfer listing data
             os = new OutputStreamWriter(dataSoc.getOutputStream());
             if (!mUser.getVirtualDirectory().printList(request.getArgument(), os)) {
                 out.write(mFtpStatus.getResponse(501, request, mUser, null));
             }
             else {
                out.write(mFtpStatus.getResponse(226, request, mUser, null));
             }
         }
         catch(IOException ex) {
             out.write(mFtpStatus.getResponse(425, request, mUser, null));
         }
         finally {
             IoUtils.close(os);
             mDataConnection.closeDataSocket();
         }
     }
     
     
     /**
      * <code>MDTM &lt;SP&gt; &lt;pathname&gt; &lt;CRLF&gt;</code><br>
      * 
      * Returns the date and time of when a file was modified.
      */
     public void doMDTM(FtpRequest request, FtpWriter out) throws IOException {
         
         // reset state
         resetState();
         
         // argument check
         if(!request.hasArgument()) {
            out.write(mFtpStatus.getResponse(501, request, mUser, null));
            return;  
         }
        
         // get filenames
         String fileName = request.getArgument();
         fileName = mUser.getVirtualDirectory().getAbsoluteName(fileName);
         String physicalName = mUser.getVirtualDirectory().getPhysicalName(fileName);
         File reqFile = new File(physicalName);

         // now print date
         if(reqFile.exists()) {
             SimpleDateFormat fmt = (SimpleDateFormat)DATE_FMT.get();
             String args[] = {fmt.format(new Date(reqFile.lastModified()))};
             out.write(mFtpStatus.getResponse(213, request, mUser, args));
         }
         else {
             out.write(mFtpStatus.getResponse(550, request, mUser, null));
         }
     } 
     
     
     /**
      * <code>MKD  &lt;SP&gt; &lt;pathname&gt; &lt;CRLF&gt;</code><br>
      *
      * This command causes the directory specified in the pathname
      * to be created as a directory (if the pathname is absolute)
      * or as a subdirectory of the current working directory (if
      * the pathname is relative).
      */
     public void doMKD(FtpRequest request, FtpWriter out) throws IOException {
        
        // reset state
        resetState(); 
         
        // argument check
        if(!request.hasArgument()) {
            out.write(mFtpStatus.getResponse(501, request, mUser, null));
            return;  
        }
        
        // get filename
        String fileName = request.getArgument();
        fileName = mUser.getVirtualDirectory().getAbsoluteName(fileName);
        String physicalName = mUser.getVirtualDirectory().getPhysicalName(fileName);
        String args[] = {fileName};
        
        // check permission
        if(!mUser.getVirtualDirectory().hasCreatePermission(physicalName, true)) {
            out.write(mFtpStatus.getResponse(450, request, mUser, args));
            return;
        }
        
        // now create directory
        if(new File(physicalName).mkdirs()) {
           out.write(mFtpStatus.getResponse(250, request, mUser, args)); 
        }
        else {
           out.write(mFtpStatus.getResponse(450, request, mUser, args));
        }
     }

     
     /**
      * <code>MODE &lt;SP&gt; <mode-code> &lt;CRLF&gt;</code><br>
      *
      * The argument is a single Telnet character code specifying
      * the data transfer modes described in the Section on
      * Transmission Modes.
      */
     public void doMODE(FtpRequest request, FtpWriter out) throws IOException {
         
         // reset state
         resetState();
         
         // argument check
         if(!request.hasArgument()) {
            out.write(mFtpStatus.getResponse(501, request, mUser, null));
            return;  
         }
         
         // set mode
         if (mUser.setMode(request.getArgument().charAt(0))) {
            out.write(mFtpStatus.getResponse(200, request, mUser, null));
         }
         else {
            out.write(mFtpStatus.getResponse(504, request, mUser, null));
         }
     }
     
     
     /**
      * <code>NLST [&lt;SP&gt; &lt;pathname&gt;] &lt;CRLF&gt;</code><br>
      *
      * This command causes a directory listing to be sent from
      * server to user site.  The pathname should specify a
      * directory or other system-specific file group descriptor; a
      * null argument implies the current directory.  The server
      * will return a stream of names of files and no other
      * information.
      */
     public void doNLST(FtpRequest request, FtpWriter out) throws IOException {
         
         Writer os = null;
         try {
            
             // reset state
             resetState();
              
             // get data connection
             out.write(mFtpStatus.getResponse(150, request, mUser, null));
             Socket dataSoc = mDataConnection.getDataSocket();
             if (dataSoc == null) {
                  out.write(mFtpStatus.getResponse(550, request, mUser, null));
                  return;
             }
             
             // print listing data
             os = new OutputStreamWriter(dataSoc.getOutputStream());
             if (!mUser.getVirtualDirectory().printNList(request.getArgument(), os)) {
                 out.write(mFtpStatus.getResponse(501, request, mUser, null));
             }
             else {
                out.write(mFtpStatus.getResponse(226, request, mUser, null));
             }
         }
         catch(IOException ex) {
             out.write(mFtpStatus.getResponse(425, request, mUser, null));
         }
         finally {
             IoUtils.close(os);
             mDataConnection.closeDataSocket();
         }
     }
     
     
     /**
      * <code>NOOP &lt;CRLF&gt;</code><br>
      *
      * This command does not affect any parameters or previously
      * entered commands. It specifies no action other than that the
      * server send an OK reply.
      */
     public void doNOOP(FtpRequest request, FtpWriter out) throws IOException {
         resetState();
         out.write(mFtpStatus.getResponse(200, request, mUser, null));
     } 
     
     
     /**
      * <code>PASS &lt;SP&gt; <password> &lt;CRLF&gt;</code><br>
      *
      * The argument field is a Telnet string specifying the user's
      * password.  This command must be immediately preceded by the
      * user name command.
      */
     public void doPASS(FtpRequest request, FtpWriter out) throws IOException {
         
         // set state variables
         if(!mbUser) {
             out.write(mFtpStatus.getResponse(500, request, mUser, null));
             resetState();
             return;
         }
         resetState();
         mbPass = true;
         
         // set user password and login
         String pass = request.hasArgument() ? request.getArgument() : "";
         mUser.setPassword(pass);
         
         // login failure - close connection
         String args[] = {mUser.getName()};
         if (mConfig.getConnectionService().login(mUser)) {
            out.write(mFtpStatus.getResponse(230, request, mUser, args));
         }
         else {
            out.write(mFtpStatus.getResponse(530, request, mUser, args));
            ConnectionService conService = mConfig.getConnectionService();
            if (conService != null) {
                conService.closeConnection(mUser.getSessionId());
            }
         }
     }
     
     
     /**
      * <code>PASV &lt;CRLF&gt;</code><br>
      *
      * This command requests the server-DTP to "listen" on a data
      * port (which is not its default data port) and to wait for a
      * connection rather than initiate one upon receipt of a
      * transfer command.  The response to this command includes the
      * host and port address this server is listening on.
      */
     public void doPASV(FtpRequest request, FtpWriter out) throws IOException {
         
         // reset state
         resetState();
         
         // set data connection
         if (!mDataConnection.setPasvCommand()) {
             out.write(mFtpStatus.getResponse(550, request, mUser, null));
             return;   
         }
         
         // get connection info
         InetAddress servAddr = mDataConnection.getInetAddress();
         if(servAddr == null) {
             servAddr = mConfig.getSelfAddress();
         }        

         int servPort = mDataConnection.getPort();
         
         // send connection info to client
         String addrStr = servAddr.getHostAddress().replace( '.', ',' ) + ',' + (servPort>>8) + ',' + (servPort&0xFF);
         String[] args = {addrStr};
         out.write(mFtpStatus.getResponse(227, request, mUser, args));
     }
     
     
     /**
      * <code>PORT &lt;SP&gt; <host-port> &lt;CRLF&gt;</code><br>
      *
      * The argument is a HOST-PORT specification for the data port
      * to be used in data connection.  There are defaults for both
      * the user and server data ports, and under normal
      * circumstances this command and its reply are not needed.  If
      * this command is used, the argument is the concatenation of a
      * 32-bit internet host address and a 16-bit TCP port address.
      * This address information is broken into 8-bit fields and the
      * value of each field is transmitted as a decimal number (in
      * character string representation).  The fields are separated
      * by commas.  A port command would be:
      *
      *   PORT h1,h2,h3,h4,p1,p2
      * 
      * where h1 is the high order 8 bits of the internet host address.
      */
     public void doPORT(FtpRequest request, FtpWriter out) throws IOException {
         
         // reset state variables
         resetState();
         
         // argument check
         if(!request.hasArgument()) {
            out.write(mFtpStatus.getResponse(501, request, mUser, null));
            return;  
         }

         StringTokenizer st = new StringTokenizer(request.getArgument(), ",");
         if(st.countTokens() != 6) {
             out.write(mFtpStatus.getResponse(510, request, mUser, null));
             return;
         }
             
         // get data server
         String dataSrvName = st.nextToken() + '.' + st.nextToken() + '.' +
                              st.nextToken() + '.' + st.nextToken();
         InetAddress clientAddr = null;

⌨️ 快捷键说明

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