1. 程式人生 > >Jakarta Commons NET(FTPClient)的簡單示例

Jakarta Commons NET(FTPClient)的簡單示例

java中使用FTP傳送檔案或者取得檔案,可以使用Jakarta Commons NET(FTPClient)的包來實現。

具體的示例如下:(例子是從網上拷貝的)

    package test.ftp;  
      
    import java.io.FileInputStream;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.io.InputStream;  
    import java.io.OutputStream;  
      
    import org.apache.commons.net.ftp.FTP;  
    import org.apache.commons.net.ftp.FTPClient;  
    import org.apache.commons.net.ftp.FTPReply;  
      
      
    public class FtpClientUtil {  
        private static final int FTP_PORT = 21;  
          
        public static void main(String[] args) {  
            try {  
                //讀入檔案  
                FileInputStream fis = new FileInputStream("c:\testftp.txt");  
                //傳送檔案到FTP伺服器
                FtpClientUtil.sendFile("localhost", FTP_PORT, "testuser", "testpassword",   "remoteFilename", fis);  
                  
                //從FTP伺服器取得檔案  
                FileOutputStream fos = new FileOutputStream("localfile");   
                FtpClientUtil.retrieveFile("localhost", FTP_PORT, "testuser", "testpassword",   "remoteFilename", fos);  
                  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
      
        //上傳檔案  
        public static void sendFile (String host,   
                int port,  
                String user,  
                String password,  
                String remoteFilename,  
                InputStream is  
                ) throws Exception {  
            FTPClient ftpclient = new FTPClient();  
              
            try {  
                //設定伺服器名和埠
                ftpclient.connect(host, port);  
                int reply = ftpclient.getReplyCode();  
                if (!FTPReply.isPositiveCompletion(reply)) {  
                    //連線錯誤的時候報錯。 
                    Exception ee = new Exception("Can't Connect to :" + host);  
                    throw ee;  
                }  
                  
                //登入  
                if (ftpclient.login(user, password) == false) {  
                    // invalid user/password  
                    Exception ee = new Exception("Invalid user/password");  
                    throw ee;  
                }  
      
                //設定傳送檔案模式  
                ftpclient.setFileType(FTP.BINARY_FILE_TYPE);  
                  
                //傳送檔案  
                ftpclient.storeFile(remoteFilename, is);                                
            } catch (IOException e) {  
                throw e;  
            } finally {  
                try {  
                    ftpclient.disconnect(); //解除連線  
                } catch (IOException e) {  
                }  
            }  
              
        }  
          
        //檔案下載  
        public static void retrieveFile(String host,   
                int port,  
                String user,  
                String password,  
                String remoteFilename,  
                OutputStream os) throws Exception {  
            FTPClient ftpclient = new FTPClient();  
              
            try {  
                //設定伺服器名和埠  
                ftpclient.connect(host, port);  
                int reply = ftpclient.getReplyCode();  
                if (!FTPReply.isPositiveCompletion(reply)) {  
                    //連線錯誤  
                    Exception ee = new Exception("Can't Connect to :" + host);  
                    throw ee;  
                }  
                  
                //登入  
                if (ftpclient.login(user, password) == false) {  
                    // invalid user/password  
                    Exception ee = new Exception("Invalid user/password");  
                    throw ee;  
                }  
      
                //設定傳送模式 
                ftpclient.setFileType(FTP.BINARY_FILE_TYPE);  
                  
                // 取得檔案  
                ftpclient.retrieveFile(remoteFilename, os);  
      
            } catch (IOException e) {  
                throw e;  
            } finally {  
                try {  
                    ftpclient.disconnect(); //解除連線 
                } catch (IOException e) {  
                }  
            }  
        }  
    }