Ejemplo n.º 1
0
  @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;
  }
Ejemplo n.º 2
0
 /**
  * The method to download a file and save it onto the local drive of the client in the specified
  * absolut path
  *
  * @param localfilename – the local absolute file name that the file needs to be saved as
  */
 public synchronized boolean downloadFile(String remoteFile, String localfilename) {
   try {
     /*
      * URL url = new URL(this.getRemoteFileUrl(remoteFile));
      * URLConnection urlFtpConnection = url.openConnection();
      * InputStream is = urlFtpConnection.getInputStream();
      * BufferedInputStream bis = new BufferedInputStream(is);
      */
     OutputStream os = new FileOutputStream(localfilename);
     return this.retrieveFile(remoteFile, os);
     /*
      * BufferedOutputStream bos = new BufferedOutputStream(os);
      * byte[] buffer = new byte[1024];
      * int readCount;
      * while ((readCount = bis.read(buffer)) > 0)
      * {
      * bos.write(buffer, 0, readCount);
      * }
      * bos.close();
      * is.close(); // close the FTP inputstream
      * logMessage("File `" + localfilename + "` is downloaded!");
      * bis.close();
      * return true;
      */
   } catch (Exception ex) {
     StringWriter sw0 = new StringWriter();
     PrintWriter p0 = new PrintWriter(sw0, true);
     ex.printStackTrace(p0);
     log.error(sw0.getBuffer().toString());
     log.exception(ex);
     return false;
   }
 }
Ejemplo n.º 3
0
  /**
   *
   *
   * <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;
    }
  }
Ejemplo n.º 4
0
  /**
   *
   *
   * <pre>
   * <b>Description:</b>
   * Constructor.
   * </pre>
   *
   * @param String sFtpHost -- name connect ftp host
   * @param String sUserName -- connect user name
   * @param String sPassword -- connect user password
   */
  public FTPHelper(Connection oConn, Logger oLog) {
    this(oConn);
    log = oLog;

    if (LogLevel.getValue(log.getLogLevel()).ordinal() > LogLevel.DEBUG.ordinal()) {
      log.setLogLevel(LogLevel.DEBUG);
    }
  }
Ejemplo n.º 5
0
  /**
   * Function retrieved file on the remote server NOTE: This function closes output stream after
   * retrieving file content
   *
   * @param fileName
   * @param fileOutput
   * @return
   * @throws SocketException
   * @throws IOException
   */
  public boolean retrieveFile(String fileName, OutputStream fileOutput)
      throws SocketException, IOException {
    boolean fileRetrieved = false;

    connect2Server();

    if (oFtp.isConnected()) {

      try {

        fileRetrieved = oFtp.retrieveFile(fileName, fileOutput);

        if (fileRetrieved) {
          logMessage("File '" + fileName + "' retrieved");
        } else {
          logError(
              "Can't retrieve file "
                  + fileName
                  + " FTP reply code: "
                  + oFtp.getReplyCode()
                  + " Ftp message: "
                  + oFtp.getReplyString());
        }
        fileOutput.close();
      } catch (IOException ioe) {
        log.warning("FTPUtil - retrieveFile(): " + ioe.toString());
        return false;
      }
    }
    this.closeFtpConnection();
    return fileRetrieved;
  }
Ejemplo n.º 6
0
  /**
   *
   *
   * <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;
    }
  }
Ejemplo n.º 7
0
  /**
   *
   *
   * <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;
    }
  }
Ejemplo n.º 8
0
  /**
   * @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;
  }
Ejemplo n.º 9
0
  private synchronized void connect2Server() throws SocketException, IOException {
    if (oFtp == null) {
      this.oFtp = new FTPClient();
    }

    if (log != null) {
      if (LogLevel.getValue(log.getLogLevel()).ordinal() > LogLevel.DEBUG.ordinal()) {
        oFtp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.err)));
      }
    }

    if (_port == 0) {
      oFtp.connect(this._sFtpHost, 21);
    } else {
      oFtp.connect(this._sFtpHost, _port);
    }
    if (!oFtp.login(this._sFtpUserName, this._sFtpPassword)) {
      logError("Can't login to server. FTP responce: " + oFtp.getReplyString());
    }

    oFtp.enterLocalPassiveMode();
  }
Ejemplo n.º 10
0
  /**
   *
   *
   * <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;
    }
  }
Ejemplo n.º 11
0
 private void logMessage(String msg) {
   if (this.log != null) {
     log.message(msg);
   }
 }
Ejemplo n.º 12
0
 private void logError(String err) {
   if (this.log != null) {
     log.error(err);
   }
 }
Ejemplo n.º 13
0
  /**
   * The method that handles file uploading, this method takes the absolute file path of a local
   * file to be uploaded to the remote FTP server, and the remote file will then be transfered to
   * the FTP server and saved as the relative path name specified in method setRemoteFile
   *
   * @param localfilename – the local absolute file name of the file in local hard drive that needs
   *     to FTP over
   */
  public synchronized boolean uploadFile(String localfilename, String remoteFile) {

    final BufferedInputStream bis;
    try {
      // URL url = new URL(this.getRemoteFileUrl(remoteFile));
      // URLConnection urlFtpConnection = url.openConnection();

      InputStream is = new FileInputStream(localfilename);
      bis = new BufferedInputStream(is);

      connect2Server();

      if (oFtp.isConnected()) {
        String fileExtension = localfilename.substring(localfilename.lastIndexOf(".") + 1);

        String[] fileExtensions = {"png", "gif", "bmp", "jpg", "jpeg", "tiff"};

        // If exporting to .csv or .tsv, add the gid parameter to specify which sheet to export
        if (Arrays.asList(fileExtensions).contains(fileExtension)) {
          oFtp.setFileType(FTP.BINARY_FILE_TYPE);
        }

        // OutputStream os = oFtp.storeFileStream(remoteFile);
        // if (os == null)
        // {
        // logError(oFtp.getReplyString());
        // }
        // BufferedOutputStream bos = new BufferedOutputStream(os);
        // byte[] buffer = new byte[1024];
        // int readCount;
        //
        // while ((readCount = bis.read(buffer)) > 0)
        // {
        // bos.write(buffer);
        // // bos.flush();
        // }
        // bos.close();

        if (!oFtp.storeFile(remoteFile, bis)) {
          logError(
              "Can't upload file "
                  + localfilename
                  + " FTP reply code: "
                  + oFtp.getReplyCode()
                  + " Ftp message: "
                  + oFtp.getReplyString());

          bis.close();
          is.close();

          this.closeFtpConnection();
          return false;
        } else {
          logMessage("File `" + localfilename + "` is uploaded!");
        }
      }

      /*
       * urlFtpConnection.getOutputStream();
       * //
       * log.message("File `" + localfilename + "` is uploaded!");
       */

      bis.close();
      is.close();
      // this.closeFtpConnection();
      return true;
    } catch (Exception ex) {
      StringWriter sw0 = new StringWriter();
      PrintWriter p0 = new PrintWriter(sw0, true);
      ex.printStackTrace(p0);
      log.error(sw0.getBuffer().toString());
      log.exception(ex);

      return false;
    }
  }