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

📄 jmyftpclient.java

📁 my ftp client basic and easy
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    public int get( String strSrcFile , String strDstFile ) throws IOException, FTPException
    {
        // get an input stream to read data from
        data = control.createDataSocket();
        DataInputStream in = new DataInputStream(data.getInputStream());
        BufferedInputStream bIn = new BufferedInputStream(in);
 
        File fTmp = new File(strDstFile);
        fTmp.delete();
        fTmp.createNewFile();
        
        RandomAccessFile fDst = new RandomAccessFile( fTmp, "rw");
 //       FileOutputStream ostream = new FileOutputStream( fTmp );
        // send the retrieve command
        String reply = control.sendCommand("RETR " + strSrcFile);
        
        // Can get a 125 or a 150 
        String[] validCodes1 = {"125", "150"};
        control.validateReply(reply, validCodes1);
        
        // do the retrieving
        int chunksize = 1024*32;
        byte [] abyBuf = new byte[chunksize];  // read chunks into
        int nRead = 0;  // size of chunk read
        int nTmpRead =0;
        // read from socket & write to file
        while (( nTmpRead = bIn.read( abyBuf , 0, chunksize)) >= 0) 
        {
        	fDst.write( abyBuf , 0 , nTmpRead );
        	nRead += nTmpRead;
        }
 //       fDst.flush();
        fDst.close();
        // close streams
        bIn.close();
        data.close();
      
        // check the control response  
        String[] validCodes2 = {"226", "250"};
        reply = control.readReply();
        control.validateReply(reply, validCodes2);
        
        return nRead;

    }

    /**
     *  Get data from the FTP server. Uses the currently
     *  set transfer mode. Retrieve as a byte array. Note
     *  that we may experience memory limitations as the
     *  entire file must be held in memory at one time.
     *
     *  @param  remoteFile  name of remote file in
     *                      current directory
     */             
    public byte[] get(String remoteFile) 
        throws IOException, FTPException {

            // get an input stream to read data from
            data = control.createDataSocket();
            DataInputStream in = new DataInputStream(data.getInputStream());
            BufferedInputStream bIn = new BufferedInputStream(in);
            
            // send the retrieve command
            String reply = control.sendCommand("RETR " + remoteFile);
            
            // Can get a 125 or a 150 
            String[] validCodes1 = {"125", "150"};
            control.validateReply(reply, validCodes1);
            
            // do the retrieving
            int chunksize = 4096;
            byte [] chunk = new byte[chunksize];  // read chunks into
            byte [] resultBuf = new byte[chunksize];  // where we place chunks
            byte [] temp = null;  // temp swap buffer
            int count;  // size of chunk read
            int bufsize = 0;  // size of resultBuf
            
            // read from socket & write to file
            while ((count = bIn.read(chunk, 0, chunksize)) >= 0) {
                
                // new buffer to hold current buf + new chunk
                temp = new byte[bufsize+count];
                
                // copy current buf to temp
                System.arraycopy(resultBuf, 0, temp, 0, bufsize);
                
                // copy new chunk onto end of temp
                System.arraycopy(chunk, 0, temp, bufsize, count);
                
                // re-assign temp buffer to buf
                resultBuf = temp;
                
                // update size of buffer
                bufsize += count;
            }

            // close streams
            try {
                bIn.close();
                data.close();
            }
            catch (IOException ignore) {} 
              
            // check the control response  
            String[] validCodes2 = {"226", "250"};
            reply = control.readReply();
            control.validateReply(reply, validCodes2);
            
            return resultBuf;
        }


    /**
     *  Run a site-specific command on the
     *  server. Support for commands is dependent
     *  on the server
     *
     *  @param  command   the site command to run    
     *  @return true if command ok, false if
     *          command not implemented
     */
    public boolean site(String command) 
        throws IOException, FTPException {
	
        // send the retrieve command
	String reply = control.sendCommand("SITE " + command);
     
        // Can get a 200 (ok) or 202 (not impl). Some
        // FTP servers return 502 (not impl)
        String[] validCodes = {"200", "202", "502"};
        control.validateReply(reply, validCodes);
	
        // return true or false? 200 is ok, 202/502 not
        // implemented
        if (reply.substring(0, 3).equals("200"))
            return true;
        else
            return false;
    }


    /**
     *  List a directory's contents
     *
     *  @param  mask  the file mask to use
     *  @return  		the sub dirs and files, start with d or f in String[i][0];
     */
    public String[] list(String mask) 
        throws IOException, FTPException {
  
    	String strValue = list( mask ,true );
    	return strValue.split( "\r\n" );
    }
    

    /**
     *  List a directory's contents
     *
     *  @param  mask  the file mask to use
     *  @param  full  true if detailed listing required
     *                false otherwise
     */
    public String list(String mask, boolean full) 
        throws IOException, FTPException {

        // first set type to ascii if binary
        boolean isBinary = false;
        if (transferType.equals(FTPTransferType.BINARY)) {            
            isBinary = true;
            setType(FTPTransferType.ASCII);
        }
  
        // get an input stream to read data from
        data = control.createDataSocket();
        InputStreamReader in = new InputStreamReader(data.getInputStream());
        BufferedReader bIn = new BufferedReader(in);
                
        // send the retrieve command
	String command = full ? "LIST ":"NLST ";
	String reply = control.sendCommand(command + mask);
     
        // Can get a 125 or a 150 
        String[] validCodes1 = {"125", "150"};
        control.validateReply(reply, validCodes1);
        
        // get the listing ... this comes on the data channel
        // do the retrieving
        int chunksize = 4096;
        char [] chunk = new char[chunksize];  // read chunks into
        char [] resultBuf = new char[chunksize];  // where we place chunks
        char [] temp = null;  // temp swap buffer
        int count;  // size of chunk read
        int bufsize = 0;  // size of resultBuf
        
        // read from socket & write to file
        while ((count = bIn.read(chunk, 0, chunksize)) >= 0) {
            
            // new buffer to hold current buf + new chunk
            temp = new char[bufsize+count];
            
            // copy current buf to temp
            System.arraycopy(resultBuf, 0, temp, 0, bufsize);
            
            // copy new chunk onto end of temp
            System.arraycopy(chunk, 0, temp, bufsize, count);
            
            // re-assign temp buffer to buf
            resultBuf = temp;
            
            // update size of buffer
            bufsize += count;
        }
              
        // close streams. NOTE! For NLST we close our end before
        // reading the reply! wu-ftpd requires this.
        try {
            bIn.close();
            data.close();
        }
        catch (IOException ignore) {}

        // check the control response  
        String[] validCodes2 = {"226", "250"};
        reply = control.readReply();
        control.validateReply(reply, validCodes2);

        // reset type
        if (isBinary)
            setType(FTPTransferType.BINARY);
        
        String result = null;
        if (resultBuf.length > 0)
            result = new String(resultBuf);
        return result;  // this is the String with the listing in it
    }        


    /**
     *  Switch debug of responses on or off
     *
     *  @param  on  true if you wish to have responses to 
     *              stdout, false otherwise
     */
    public void debugResponses(boolean on) {

        control.debugResponses(on);
    }


    /**
     *  Get the current transfer type
     *
     *  @return  the current type of the transfer,
     *           i.e. BINARY or ASCII  
     */             
    public FTPTransferType getType() { 
        
        return transferType;
    }
        
    
    /**
     *  Set the transfer type
     *
     *  @param  type  the transfer type to
     *                set the server to
     */             
    public void setType(FTPTransferType type)  
        throws IOException, FTPException {
        
        // determine the character to send
        String typeStr = FTPTransferType.ASCII_CHAR;
        if (type.equals(FTPTransferType.BINARY))
            typeStr = FTPTransferType.BINARY_CHAR;
        
        // send the command
        String reply = control.sendCommand("TYPE " + typeStr);
        control.validateReply(reply, "200");    
        
        // record the type
        transferType = type;            
    }       


    /**
     *  Delete the specified remote file
     *
     *  @param  remoteFile  name of remote file to
     *                      delete
     */             
    public void delete(String remoteFile) 
        throws IOException, FTPException {
        
        String reply = control.sendCommand("DELE " + remoteFile);
        control.validateReply(reply, "250");    
    }


    /**
     *  Rename a file or directory
     *
     * @param from  name of file or directory to rename
     * @param to    intended name
     */
    public void rename(String from, String to)
        throws IOException, FTPException {
        
        String reply = control.sendCommand("RNFR " + from);
	control.validateReply(reply, "350");
        
        reply = control.sendCommand("RNTO " + to);
        control.validateReply(reply, "250");
    }
    

    /**
     *  Delete the specified remote working directory 
     *
     *  @param  dir  name of remote directory to
     *               delete
     */             
    public void rmdir(String dir)  
        throws IOException, FTPException {
        
        String reply = control.sendCommand("RMD " + dir);
        control.validateReply(reply, "250");        
    }
        

    /**
     *  Create the specified remote working directory 
     *
     *  @param  dir  name of remote directory to
     *               create
     */             
    public void mkdir(String dir)  
        throws IOException, FTPException {
        
        String reply = control.sendCommand("MKD " + dir);
        control.validateReply(reply, "257");    
    }
        
    
    /**
     *  Change the remote working directory to
     *  that supplied
     *
     *  @param  dir  name of remote directory to
     *               change to
     */             
    public void chdir(String dir) 
        throws IOException, FTPException {
  
        String reply = control.sendCommand("CWD " + dir);
        control.validateReply(reply, "250");
    }
        

    /**
     *  Get the current remote working directory 
     *
     *  @return   the current working directory
     */             
    public String pwd()  
        throws IOException, FTPException {
        
        String reply = control.sendCommand("PWD");
        control.validateReply(reply, "257");
        return reply;
    }


    /**
     *  Get the type of the OS at the server
     *
     *  @return   the type of server OS
     */             
    public String system()  
        throws IOException, FTPException {
        
        String reply = control.sendCommand("SYST");
        control.validateReply(reply, "215");
        return reply;
    }


    /**
     *  Quit the FTP session
     *      
     */             
    public void quit() 
        throws IOException, FTPException {
        
        String reply = control.sendCommand("QUIT" + FTPControlSocket.EOL);
        control.validateReply(reply, "221");    
        
        control.logout();
        control = null;
    }
    
}

/**
 *  Supports client-side FTP operations
 *
 *  @author             Bruce Blackshaw
 *      @version        $Revision: 1.1.1.1 $
 *
 */
 class FTPControlSocket {
 
     /**
      *  Revision control id
      */
     private static String cvsId = "$Id: FTPControlSocket.java,v 1.1.1.1 2001/10/28 06:19:32 idanso Exp $";
     
     /**

⌨️ 快捷键说明

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