ftpclient.java

来自「apache推出的net包」· Java 代码 · 共 1,527 行 · 第 1/5 页

JAVA
1,527
字号
     *     ftp.logout();     *     ftp.disconnect();     *     System.err.println("File transfer failed.");     *     System.exit(1);     * }     * Util.copyStream(input, output);     * input.close();     * output.close();     * // Must call completePendingCommand() to finish command.     * if(!ftp.completePendingCommand()) {     *     ftp.logout();     *     ftp.disconnect();     *     System.err.println("File transfer failed.");     *     System.exit(1);     * }     * </pre>     * <p>     * @return True if successfully completed, false if not.     * @exception FTPConnectionClosedException     *      If the FTP server prematurely closes the connection as a result     *      of the client being idle or some other reason causing the server     *      to send FTP reply code 421.  This exception may be caught either     *      as an IOException or independently as itself.     * @exception IOException  If an I/O error occurs while either sending a     *      command to the server or receiving a reply from the server.     ***/    public boolean completePendingCommand() throws IOException    {        return FTPReply.isPositiveCompletion(getReply());    }    /***     * Retrieves a named file from the server and writes it to the given     * OutputStream.  This method does NOT close the given OutputStream.     * If the current file type is ASCII, line separators in the file are     * converted to the local representation.     * <p>     * @param remote  The name of the remote file.     * @param local   The local OutputStream to which to write the file.     * @return True if successfully completed, false if not.     * @exception FTPConnectionClosedException     *      If the FTP server prematurely closes the connection as a result     *      of the client being idle or some other reason causing the server     *      to send FTP reply code 421.  This exception may be caught either     *      as an IOException or independently as itself.     * @exception CopyStreamException  If an I/O error occurs while actually     *      transferring the file.  The CopyStreamException allows you to     *      determine the number of bytes transferred and the IOException     *      causing the error.  This exception may be caught either     *      as an IOException or independently as itself.     * @exception IOException  If an I/O error occurs while either sending a     *      command to the server or receiving a reply from the server.     ***/    public boolean retrieveFile(String remote, OutputStream local)    throws IOException    {        InputStream input;        Socket socket;        if ((socket = _openDataConnection_(FTPCommand.RETR, remote)) == null)            return false;        input = new BufferedInputStream(socket.getInputStream(),                                        getBufferSize());        if (__fileType == ASCII_FILE_TYPE)          input = new FromNetASCIIInputStream(input);        // Treat everything else as binary for now        try        {            Util.copyStream(input, local, getBufferSize(),                            CopyStreamEvent.UNKNOWN_STREAM_SIZE, null,                            false);        }        catch (IOException e)        {            try            {                socket.close();            }            catch (IOException f)            {}            throw e;        }        socket.close();        return completePendingCommand();    }    /***     * Returns an InputStream from which a named file from the server     * can be read.  If the current file type is ASCII, the returned     * InputStream will convert line separators in the file to     * the local representation.  You must close the InputStream when you     * finish reading from it.  The InputStream itself will take care of     * closing the parent data connection socket upon being closed.  To     * finalize the file transfer you must call     * {@link #completePendingCommand  completePendingCommand } and     * check its return value to verify success.     * <p>     * @param remote  The name of the remote file.     * @return An InputStream from which the remote file can be read.  If     *      the data connection cannot be opened (e.g., the file does not     *      exist), null is returned (in which case you may check the reply     *      code to determine the exact reason for failure).     * @exception FTPConnectionClosedException     *      If the FTP server prematurely closes the connection as a result     *      of the client being idle or some other reason causing the server     *      to send FTP reply code 421.  This exception may be caught either     *      as an IOException or independently as itself.     * @exception IOException  If an I/O error occurs while either sending a     *      command to the server or receiving a reply from the server.     ***/    public InputStream retrieveFileStream(String remote) throws IOException    {        InputStream input;        Socket socket;        if ((socket = _openDataConnection_(FTPCommand.RETR, remote)) == null)            return null;        input = socket.getInputStream();        if (__fileType == ASCII_FILE_TYPE) {          // We buffer ascii transfers because the buffering has to          // be interposed between FromNetASCIIOutputSream and the underlying          // socket input stream.  We don't buffer binary transfers          // because we don't want to impose a buffering policy on the          // programmer if possible.  Programmers can decide on their          // own if they want to wrap the SocketInputStream we return          // for file types other than ASCII.          input = new BufferedInputStream(input,                                          getBufferSize());          input = new FromNetASCIIInputStream(input);        }        return new org.apache.commons.net.io.SocketInputStream(socket, input);    }    /***     * Stores a file on the server using the given name and taking input     * from the given InputStream.  This method does NOT close the given     * InputStream.  If the current file type is ASCII, line separators in     * the file are transparently converted to the NETASCII format (i.e.,     * you should not attempt to create a special InputStream to do this).     * <p>     * @param remote  The name to give the remote file.     * @param local   The local InputStream from which to read the file.     * @return True if successfully completed, false if not.     * @exception FTPConnectionClosedException     *      If the FTP server prematurely closes the connection as a result     *      of the client being idle or some other reason causing the server     *      to send FTP reply code 421.  This exception may be caught either     *      as an IOException or independently as itself.     * @exception CopyStreamException  If an I/O error occurs while actually     *      transferring the file.  The CopyStreamException allows you to     *      determine the number of bytes transferred and the IOException     *      causing the error.  This exception may be caught either     *      as an IOException or independently as itself.     * @exception IOException  If an I/O error occurs while either sending a     *      command to the server or receiving a reply from the server.     ***/    public boolean storeFile(String remote, InputStream local)    throws IOException    {        return __storeFile(FTPCommand.STOR, remote, local);    }    /***     * Returns an OutputStream through which data can be written to store     * a file on the server using the given name.  If the current file type     * is ASCII, the returned OutputStream will convert line separators in     * the file to the NETASCII format  (i.e., you should not attempt to     * create a special OutputStream to do this).  You must close the     * OutputStream when you finish writing to it.  The OutputStream itself     * will take care of closing the parent data connection socket upon being     * closed.  To finalize the file transfer you must call     * {@link #completePendingCommand  completePendingCommand } and     * check its return value to verify success.     * <p>     * @param remote  The name to give the remote file.     * @return An OutputStream through which the remote file can be written.  If     *      the data connection cannot be opened (e.g., the file does not     *      exist), null is returned (in which case you may check the reply     *      code to determine the exact reason for failure).     * @exception FTPConnectionClosedException     *      If the FTP server prematurely closes the connection as a result     *      of the client being idle or some other reason causing the server     *      to send FTP reply code 421.  This exception may be caught either     *      as an IOException or independently as itself.     * @exception IOException  If an I/O error occurs while either sending a     *      command to the server or receiving a reply from the server.     ***/    public OutputStream storeFileStream(String remote) throws IOException    {        return __storeFileStream(FTPCommand.STOR, remote);    }    /***     * Appends to a file on the server with the given name, taking input     * from the given InputStream.  This method does NOT close the given     * InputStream.  If the current file type is ASCII, line separators in     * the file are transparently converted to the NETASCII format (i.e.,     * you should not attempt to create a special InputStream to do this).     * <p>     * @param remote  The name of the remote file.     * @param local   The local InputStream from which to read the data to     *                be appended to the remote file.     * @return True if successfully completed, false if not.     * @exception FTPConnectionClosedException     *      If the FTP server prematurely closes the connection as a result     *      of the client being idle or some other reason causing the server     *      to send FTP reply code 421.  This exception may be caught either     *      as an IOException or independently as itself.     * @exception CopyStreamException  If an I/O error occurs while actually     *      transferring the file.  The CopyStreamException allows you to     *      determine the number of bytes transferred and the IOException     *      causing the error.  This exception may be caught either     *      as an IOException or independently as itself.     * @exception IOException  If an I/O error occurs while either sending a     *      command to the server or receiving a reply from the server.     ***/    public boolean appendFile(String remote, InputStream local)    throws IOException    {        return __storeFile(FTPCommand.APPE, remote, local);    }    /***     * Returns an OutputStream through which data can be written to append     * to a file on the server with the given name.  If the current file type     * is ASCII, the returned OutputStream will convert line separators in     * the file to the NETASCII format  (i.e., you should not attempt to     * create a special OutputStream to do this).  You must close the     * OutputStream when you finish writing to it.  The OutputStream itself     * will take care of closing the parent data connection socket upon being     * closed.  To finalize the file transfer you must call     * {@link #completePendingCommand  completePendingCommand } and     * check its return value to verify success.     * <p>     * @param remote  The name of the remote file.     * @return An OutputStream through which the remote file can be appended.     *      If the data connection cannot be opened (e.g., the file does not     *      exist), null is returned (in which case you may check the reply     *      code to determine the exact reason for failure).     * @exception FTPConnectionClosedException     *      If the FTP server prematurely closes the connection as a result     *      of the client being idle or some other reason causing the server     *      to send FTP reply code 421.  This exception may be caught either     *      as an IOException or independently as itself.     * @exception IOException  If an I/O error occurs while either sending a     *      command to the server or receiving a reply from the server.     ***/    public OutputStream appendFileStream(String remote) throws IOException    {        return __storeFileStream(FTPCommand.APPE, remote);    }    /***     * Stores a file on the server using a unique name derived from the     * given name and taking input     * from the given InputStream.  This method does NOT close the given     * InputStream.  If the current file type is ASCII, line separators in     * the file are transparently converted to the NETASCII format (i.e.,     * you should not attempt to create a special InputStream to do this).     * <p>     * @param remote  The name on which to base the unique name given to     *                the remote file.     * @param local   The local InputStream from which to read the file.     * @return True if successfully completed, false if not.     * @exception FTPConnectionClosedException     *      If the FTP server prematurely closes the connection as a result     *      of the client being idle or some other reason causing the server     *      to send FTP reply code 421.  This exception may be caught either     *      as an IOException or independently as itself.     * @exception CopyStreamException  If an I/O error occurs while actually     *      transferring the file.  The CopyStreamException allows you to     *      determine the number of bytes transferred and the IOException     *      causing the error.  This exception may be caught either     *      as an IOException or independently as itself.     * @exception IOException  If an I/O error occurs while either sending a     *      command to the server or receiving a reply from the server.     ***/    public boolean storeUniqueFile(String remote, InputStream local)    throws IOException    {        return __storeFile(FTPCommand.STOU, remote, local);    }    /***     * Returns an OutputStream through which data can be written to store     * a file on the server using a unique name derived from the given name.     * If the current file type     * is ASCII, the returned OutputStream will convert line separators in     * the file to the NETASCII format  (i.e., you should not attempt to     * create a special OutputStream to do this).  You must close the     * OutputStream when you finish writing to it.  The OutputStream itself     * will take care of closing the parent data connection socket upon being     * closed.  To finalize the file transfer you must call     * {@link #completePendingCommand  completePendingCommand } and     * check its return value to verify success.     * <p>     * @param remo

⌨️ 快捷键说明

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