Java工具-檢驗ftp伺服器的指定檔案是否存在

來源:互聯網
上載者:User

標籤:ram   return   ftp伺服器   mode   cat   coding   ==   cep   NPU   

import org.apache.commons.net.ftp.FTP;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPReply;/***  檢驗指定路徑的檔案是否存在ftp伺服器中* @param filePath--指定絕對路徑的檔案* @param user--ftp伺服器登陸使用者名稱* @param passward--ftp伺服器登陸密碼* @param ip--ftp的IP地址* @param port--ftp的連接埠號碼* @return*/public static boolean isFTPFileExist(String filePath,String user,String passward,String ip,int port){        FTPClient ftp = new FTPClient();        try {    // 串連ftp伺服器            ftp.connect(ip, port);            // 登陸                ftp.login(user, passward);           // 檢驗登陸操作的返回碼是否正確            if(!FTPReply.isPositiveCompletion(ftp.getReplyCode())){                ftp.disconnect();                return false;            }                        ftp.enterLocalActiveMode();            // 設定檔案類型為二進位,與ASCII有區別            ftp.setFileType(FTP.BINARY_FILE_TYPE);            // 設定編碼格式            ftp.setControlEncoding("GBK");                        // 提取絕對位址的目錄以及檔案名稱            filePath = filePath.replace("ftp://"+ip+":"+port+"/", "");            String dir = filePath.substring(0, filePath.lastIndexOf("/"));            String file = filePath.substring(filePath.lastIndexOf("/")+1);                        // 進入檔案所在目錄,注意編碼格式,以能夠正確識別中文目錄            ftp.changeWorkingDirectory(new String(dir.getBytes("GBK"),FTP.DEFAULT_CONTROL_ENCODING));            // 檢驗檔案是否存在            InputStream is = ftp.retrieveFileStream(new String(file.getBytes("GBK"),FTP.DEFAULT_CONTROL_ENCODING));            if(is == null || ftp.getReplyCode() == FTPReply.FILE_UNAVAILABLE){                return false;            }                        if(is != null){                is.close();                ftp.completePendingCommand();            }            return true;        } catch (Exception e) {            e.printStackTrace();        }finally{            if(ftp != null){                try {                    ftp.disconnect();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return false;}

 

Java工具-檢驗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.