/** * * * <pre> * <b>Description:</b> * Function remove folder from ftp * <b>Creation date: </b>02.05.08 * <b>Modification date: </b>02.05.08 * </pre> * * @param String sDirPath � removed directory * @author Vitalii Fedorets * @return boolean � true if success */ public boolean removeFolder(String sDirPath) throws Exception { try { File oDir = new File(sDirPath); FTPClient oFtp = this.initFtpClientLoginChangeWorkingDirectory(sDirPath); oFtp.changeWorkingDirectory(oDir.getParent().replace("\\", "/")); int iReply = oFtp.getReplyCode(); if (!FTPReply.isPositiveCompletion(iReply)) { log.setError("Not change work directory to `" + oDir.getParent().replace("\\", "/") + "`"); oFtp.disconnect(); return false; } if (!oFtp.removeDirectory(sDirPath)) { log.setError("Could not remove directory `" + sDirPath + "`"); oFtp.disconnect(); return false; } return true; } catch (Exception oEx) { throw oEx; } }
@Deprecated private FTPClient initFtpClientAndLogin() throws SocketException, IOException { if (oFtp != null) { return oFtp; } FTPClient oFtp = new FTPClient(); int iReply; oFtp.connect(_sFtpHost); iReply = oFtp.getReplyCode(); if (!FTPReply.isPositiveCompletion(iReply)) { log.setError("Could not connect to ftp host" + _sFtpHost); return null; } if (!oFtp.login(_sFtpUserName, _sFtpPassword)) { log.error( "Could not login with user `" + _sFtpUserName + "` and password `" + _sFtpPassword + "`"); oFtp.disconnect(); return null; } oFtp.enterLocalPassiveMode(); return oFtp; }
/** * * * <pre> * <b>Description:</b> * Function connect to ftp and get attribute value from xml file by tag name * <b>Creation date: </b>02.07.2008 * <b>Modification date: </b>02.07.2008 * </pre> * * @param String sDirPath -- dirrectory where file located * @param String sFileName -- XML file name * @param String sTag -- searching tag in file * @param String sAttribute -- tag attribute * @author Oleksii Zozulenko. * @return String sAttrValue -- attribute value, or null if error * @exception Exception */ public String getXMLAttributeValue( String sDirPath, String sFileName, String sTag, String sAttribute) throws Exception { try { String sAttrValue = null; FTPClient oFtp = this.initFtpClientLoginChangeWorkingDirectory(sDirPath); ByteArrayOutputStream oBuf = new ByteArrayOutputStream(); if (!oFtp.retrieveFile(sFileName, oBuf)) { log.setError("Could not read file `" + sFileName + "`"); oFtp.disconnect(); return null; } oFtp.disconnect(); String sBuf = oBuf.toString(); oBuf.close(); if (!sBuf.contains(sTag)) { log.setError("File `" + sFileName + "` not contains tag `" + sTag + "`"); return null; } sBuf = sBuf.substring(sBuf.indexOf(sTag)); if (!sBuf.contains(sAttribute)) { log.setError( "File `" + sFileName + "` not contains tag `" + sTag + "` with attribute `" + sAttribute + "`"); return null; } sBuf = sBuf.substring(sBuf.indexOf(sAttribute + "=\"") + (sAttribute + "=\"").length()); sAttrValue = sBuf.substring(0, sBuf.indexOf("\"")); return sAttrValue; } catch (Exception oEx) { throw oEx; } }
/** * * * <pre> * <b>Description:</b> * Function create ftp folder * <b>Creation date: </b>02.05.08 * <b>Modification date: </b>02.05.08 * </pre> * * @param String sDirName � directory name * @author Vitalii Fedorets * @return boolean � true if success */ public boolean createFolder(String sDirName) throws Exception { try { FTPClient oFtp = this.initFtpClientLoginChangeWorkingDirectory(sDirName); if (!oFtp.makeDirectory(sDirName)) { log.setError("Could not create directory `" + sDirName + "`"); oFtp.disconnect(); return false; } oFtp.disconnect(); return true; } catch (Exception oEx) { throw oEx; } }
/** * @param sDirPath * @return * @throws SocketException * @throws IOException */ private FTPClient initFtpClientLoginChangeWorkingDirectory(String sDirPath) throws SocketException, IOException { if (oFtp != null) { return oFtp; } oFtp = initFtpClientAndLogin(); int iReply; oFtp.changeWorkingDirectory(sDirPath); iReply = oFtp.getReplyCode(); if (!FTPReply.isPositiveCompletion(iReply)) { log.setError("Not change work directory to `" + sDirPath + "`"); oFtp.disconnect(); return null; } return oFtp; }
/** * * * <pre> * <b>Description:</b> * Function check file content on ftp folder * <b>Creation date: </b>02.05.08 * <b>Modification date: </b>02.05.08 * </pre> * * @param String sDirPath � directory contains file * @param String sFileName � file name * @param String sText - text which contains in file * @author Vitalii Fedorets * @return boolean � true if file contains text */ public boolean checkTextInFile(String sDirPath, String sFileName, String sText) throws Exception { try { FTPClient oFtp = this.initFtpClientLoginChangeWorkingDirectory(sDirPath); ByteArrayOutputStream oBuf = new ByteArrayOutputStream(); if (!oFtp.retrieveFile(sFileName, oBuf)) { log.setError("Could not read file `" + sFileName + "`"); oFtp.disconnect(); return false; } oFtp.disconnect(); String sBuf = oBuf.toString(); oBuf.close(); return sBuf.contains(sText); } catch (Exception oEx) { throw oEx; } }