標籤: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伺服器的指定檔案是否存在