org.apache.commons.net工具下 對ftp的簡單操作

來源:互聯網
上載者:User

標籤:blog   io   os   使用   java   ar   檔案   div   sp   

/** * 串連ftp伺服器 * @param ipIP地址 * @param port連接埠號碼 * @param username使用者名稱 * @param password密碼 * @param ftpPathftp子目錄 * @throws IOException */public int connectServer(String ip, int port, String username,String password, String ftpPath) throws IOException {system.out.println(this, "=============all_attributes==========="+"ip_"+ip+"port_"+port+"username_"+username+"password_"+password+"ftpPath_"+ftpPath);int reply;int connectFlag = 1;try {ftpClient.connect(ip, port);//串連FTP伺服器     //如果採用預設連接埠,可以使用ftp.connect(url)的方式直接連接FTP伺服器 ftpClient.login(username, password); reply = ftpClient.getReplyCode(); if(!FTPReply.isPositiveCompletion(reply)){ ftpClient.disconnect(); connectFlag = 0; return connectFlag; }  ftpClient.changeWorkingDirectory(ftpPath);//轉移到FTP伺服器目錄   system.out.println(this, "=============connet_ftp_is_ok_falg_"+connectFlag+"===========");} catch (Exception e) {system.out.println(this, "=============connet_ftp_is_error"+connectFlag+"===========");}return connectFlag;}----------------------------------------------------------------------------------------------------------------------------------------/** * upload 上傳檔案 *  * @throws java.lang.Exception * @return -1 檔案不存在 -2 檔案內容為空白 >0 成功上傳,返迴文件的大小 * @param newname上傳後的新檔案名稱 * @param filePath  上傳的檔案的路徑 */public long upload(String localPath, String newFileName) throws Exception {long result = 0;try {File file = new File(localPath);if (!file.exists()) {result = -1;//檔案不存在}if (file.length() == 0) {result = -2;//檔案內容為空白}//result = file.length(); is = new FileInputStream(file);ftpClient.storeFile(newFileName, is); ftpClient.logout();system.out.println(this, "========="+newFileName+"檔案上傳成功==========");}catch (Exception e) {e.printStackTrace();system.out.println(this, "========="+newFileName+"檔案上傳失敗==========");}finally{} return result;}---------------------------------------------------------------------------------/** * 通過檔案路徑上傳 * @param filePath * @return * @throws Exception */public long upload(String filePath) throws Exception {String newname = "";if (filePath.indexOf("/") > -1) {newname = filePath.substring(filePath.lastIndexOf("/") + 1);} else {newname = filePath;}return upload(filePath, newname);}----------------------------------------------/** *  * closeServer *  * 斷開與ftp伺服器的連結 *  * @throws java.io.IOException */public void closeServer() throws IOException{try {if (is != null) {is.close();}if (ftpClient != null){ftpClient.disconnect();}} catch (IOException e) {e.printStackTrace();}}-------------------------------------------------/**     * 讀取文字檔.     *      */    public static String readTxtFile(File filePathAndName){        String read;        FileReader fileread;        try {            fileread = new FileReader(filePathAndName);            bufread = new BufferedReader(fileread);            try {                while ((read = bufread.readLine()) != null) {                    readStr = readStr + read+ "\r\n";                }            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        System.out.println("檔案內容是:"+ "\r\n" + readStr);        return readStr;    }------------------------------------------------------    /**     * 寫檔案.     *      */    public static void writeTxtFile(String newStr,File filePathAndName) throws IOException{        //先讀取原有檔案內容,然後進行寫入操作        String filein = newStr + "\r\n" + readStr + "\r\n";        RandomAccessFile mm = null;        try {            mm = new RandomAccessFile(filePathAndName, "rw");            mm.writeBytes(filein);        } catch (IOException e1) {            // TODO 自動產生 catch 塊            e1.printStackTrace();        } finally {            if (mm != null) {                try {                    mm.close();                } catch (IOException e2) {                    // TODO 自動產生 catch 塊                    e2.printStackTrace();                }            }        }    }----------------------------------------------------/** *  * downloadFile *  * 下載ftp伺服器的資源 * @param   remoteFileName  待下載檔案名稱 * @param  localDires 本裡存放路徑 * @param  ftpPath ftp伺服器存放要下載檔案的子目錄 *  * @throws java.io.IOException */    public  void downloadFile(String remoteFileName,String localDires , String ftpPath ) throws IOException{        String strfilepath = localDires +"/"+ remoteFileName;    //寫入地址 檔案儲存路徑        BufferedOutputStream bf = null;           boolean success=false;               try {         ftpClient.changeWorkingDirectory(ftpPath);            bf= new BufferedOutputStream(new FileOutputStream(strfilepath));                        system.out.println(this, "========="+remoteFileName+"開始下載==========");                        success = ftpClient.retrieveFile(remoteFileName,bf);  //下載操作//            ftpClient.logout();            if(success==true)            {            system.out.println(this, "========="+remoteFileName+"成功下載到=========="+localDires);            }                    } catch (IOException e1) {            // TODO 自動產生 catch 塊            e1.printStackTrace();        } finally {            if (null != bf) {                try {                bf.flush();                    bf.close();                } catch (IOException e2) {                    // TODO 自動產生 catch 塊                    e2.printStackTrace();                }            }        }         if(success=false)         {         system.out.println(this, "========="+remoteFileName+"下載失敗==========");         }    }        -----------------------------------------

 

org.apache.commons.net工具下 對ftp的簡單操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.