Ejemplo n.º 1
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.º 2
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;
    }
  }