public InputStream retrieveFileStream(String relPath, long restartOffset) throws IOException {
    try {
      FTPClient client = getFtpClient();
      client.setRestartOffset(restartOffset);
      return client.retrieveFileStream(relPath);
    } catch (IOException e) {
      disconnect();

      FTPClient client = getFtpClient();
      client.setRestartOffset(restartOffset);
      return client.retrieveFileStream(relPath);
    }
  }
Example #2
0
 public FSDataInputStream open(Path file, int bufferSize) throws IOException {
   FTPClient client = connect();
   Path workDir = new Path(client.printWorkingDirectory());
   Path absolute = makeAbsolute(workDir, file);
   FileStatus fileStat = getFileStatus(client, absolute);
   if (fileStat.isDirectory()) {
     disconnect(client);
     throw new IOException("Path " + file + " is a directory.");
   }
   client.allocate(bufferSize);
   Path parent = absolute.getParent();
   // Change to parent directory on the
   // server. Only then can we read the
   // file
   // on the server by opening up an InputStream. As a side effect the working
   // directory on the server is changed to the parent directory of the file.
   // The FTP client connection is closed when close() is called on the
   // FSDataInputStream.
   client.changeWorkingDirectory(parent.toUri().getPath());
   InputStream is = client.retrieveFileStream(file.getName());
   FSDataInputStream fis = new FSDataInputStream(new FTPInputStream(is, client, statistics));
   if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
     // The ftpClient is an inconsistent state. Must close the stream
     // which in turn will logout and disconnect from FTP server
     fis.close();
     throw new IOException("Unable to open file: " + file + ", Aborting");
   }
   return fis;
 }
Example #3
0
  /**
   * Download a file from the server
   *
   * @param fileName
   * @param isTextFile
   * @return
   */
  public boolean getFile(String remoteFileName, String localFileName, boolean isTextFile)
      throws FtpException {

    byte[] data = new byte[BUFFER];
    int count;
    File tmpFile =
        new File(localFileName.substring(0, localFileName.lastIndexOf(File.separatorChar)));

    if (!tmpFile.exists()) {
      tmpFile.mkdirs();
    }

    try {
      if (isTextFile) {
        ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
      } else {
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
      }

      boolean isValidWrt =
          ftp.retrieveFile(
              remoteFileName, new BufferedOutputStream(new FileOutputStream(localFileName)));
      if (!isValidWrt) {
        close();
        throw new FtpException("remote file or local file is not exist !");
      }
      InputStream ftpReader = ftp.retrieveFileStream(remoteFileName);
      if (ftpReader == null) {
        throw new FtpException("no file");
      }
      BufferedInputStream downloadFileIn = new BufferedInputStream(ftpReader, BUFFER);
      BufferedOutputStream downloadFileOut =
          new BufferedOutputStream(new FileOutputStream(localFileName), BUFFER);
      while ((count = downloadFileIn.read(data, 0, BUFFER)) != -1) {
        downloadFileOut.write(data, 0, count);
        downloadFileOut.flush();
      }
      ftpReader.close();
      downloadFileIn.close();
      downloadFileOut.close();
      if (!ftp.completePendingCommand()) {
        close();
        throw new FtpException("remote file or local file is not exist!");
      }

    } catch (Exception ex) {
      close();
      ex.printStackTrace();
      logger.info(
          "\\== ERROR WHILE EXECUTE FTP ==// (File Name:"
              + remoteFileName
              + " ==> "
              + localFileName
              + ") >>>>>>>>>>>>>"
              + ex.getLocalizedMessage());
      throw new FtpException("ftp file exception:" + ex.getMessage());
    }
    return true;
  }
Example #4
0
  /**
   * Method that downloads a file from the FTP server.
   *
   * @param filepath is the absolute path of the file in the FTP server
   * @return the file content as byte[]
   * @throws IOException if an i/o error occurs.
   */
  public byte[] downloadFile(String filepath) throws IOException {
    login();

    FTPFile ftpFile = ftpClient.listFiles(filepath)[0];

    // file exists on FTP server?
    if (ftpFile == null) {
      throw new IOException("NULL FILE POINTER IN THE FTP SERVER");
    }

    // file is a directory?
    if (ftpFile.isDirectory()) {
      throw new IOException("FILE POINTER IS A DIRECTORY");
    }

    // its a file and exists. start download stream...
    InputStream is = ftpClient.retrieveFileStream(filepath);

    // how the server replied to the fetch command?
    int ftpReplyCode = ftpClient.getReplyCode();

    // denied?
    if (FTPReply.isNegativePermanent(ftpReplyCode)) {
      throw new IOException("SERVER FTP:REQUEST DENIED");

      // can we try again?
    } else if (FTPReply.isNegativeTransient(ftpReplyCode)) {
      // close the already open stream before try again...
      if (is != null) {
        is.close();
      }
      return downloadFile(filepath);
    }

    // server accepted the command
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // copying the file
    IOUtils.copy(is, baos);

    // closing open streams
    is.close();
    baos.flush();
    baos.close();

    // transaction is successful?
    boolean transactionCompleted = ftpClient.completePendingCommand();
    if (!transactionCompleted) {
      return downloadFile(filepath);
    }

    // we got the file
    logout();
    return baos.toByteArray();
  }
Example #5
0
  public InputStream getInputStream() {
    if (isFolder()) {
      return null;
    }

    InputStream is = null;
    try {
      String remote = getAbsolutePath();
      remote = recode(remote);
      is = ftpClient.retrieveFileStream(remote);
    } catch (IOException e) {
      throw new VFSRuntimeException("获取输入流异常", e);
    }

    return is;
  }
Example #6
0
  private static void ftpStuff() {
    try {

      FTPClient ftp = new FTPClient();
      ftp.connect("ftp.ncbi.nih.gov");

      System.out.println(ftp.getReplyString());

      ftp.login("anonymous", "*****@*****.**");

      System.out.println("before list files...");

      // ftp.li

      FTPFile[] files = ftp.listFiles(BASE_FOLDER);

      System.out.println(files.length);

      for (FTPFile file : files) {

        if (file.getName().endsWith(".gbff.gz")) {

          StringWriter writer = null;
          String charset = "ASCII";

          GZIPInputStream inputStream =
              new GZIPInputStream(ftp.retrieveFileStream(BASE_FOLDER + "/" + file.getName()));

          System.out.println("ftp.getControlEncoding() = " + ftp.getControlEncoding());

          Reader decoder = new InputStreamReader(inputStream, charset);
          BufferedReader buffered = new BufferedReader(decoder);

          String line = null;

          while ((line = buffered.readLine()) != null) {
            System.out.println("line = " + line);
          }

          System.exit(0);
        }
      }

    } catch (Exception ex) {
      Logger.getLogger(ImportGenBank.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
 public static void main(String[] args) {
   String server = "localhost";
   int port = 16885;
   String user = "******";
   String pass = "******";
   FTPClient ftpClient = new FTPClient();
   try {
     ftpClient.connect(server, port);
     showServerReply(ftpClient);
     int replyCode = ftpClient.getReplyCode();
     if (!FTPReply.isPositiveCompletion(replyCode)) {
       System.out.println("Operation failed. Server reply code: " + replyCode);
       return;
     }
     boolean success = ftpClient.login(user, pass);
     ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
     showServerReply(ftpClient);
     if (!success) {
       System.out.println("Could not login to the server");
       return;
     } else {
       System.out.println("LOGGED IN SERVER");
       ftpClient.changeWorkingDirectory("C:\\Users\\Dileep\\Desktop\\temp");
       // File outFile = new File("C:\\Users\\Dileep\\Desktop\\temp\\newFolder","NewFile.txt");
       ftpClient.enterLocalPassiveMode();
       // FileOutputStream outputStream = new FileOutputStream(outFile);
       InputStream fileInputStream = ftpClient.retrieveFileStream("Dileep.txt");
       manipulateFile(fileInputStream);
       /*if (ftpClient.retrieveFile("Dileep.txt", outputStream)){
       	outputStream.close();
       }*/
     }
     ftpClient.logout();
   } catch (IOException ex) {
     System.out.println("Oops! Something wrong happened");
     ex.printStackTrace();
   } finally {
     try {
       ftpClient.disconnect();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
 @Override
 protected InputStream openInputStream(final String file) throws IOException {
   try {
     ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
     final FTPFile[] files = ftpClient.listFiles(file);
     if (files.length == 0) throw new AccessDeniedException(file);
     if (files.length > 1) throw new IOException(file + " is a directory");
     if (files[0].isDirectory()) throw new AccessDeniedException(file);
     ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
     final InputStream ret = ftpClient.retrieveFileStream(file);
     if (ret == null)
       throw new IOException(
           "cannot open stream to file (server " + "reply " + ftpClient.getReplyCode());
     return ret;
   } catch (FTPConnectionClosedException e) {
     status = Status.DEAD;
     throw new IOException("service unavailable", e);
   }
 }
  /**
   * download file from remote host and display the downloaded percentage
   *
   * @param folder remote directory
   * @param fileName the file you want to download
   * @param destfolder the destination folder you will store the file
   */
  public void downloadFileInProgress(String folder, String fileName, String destfolder) {
    try {
      ftp.enterLocalPassiveMode();
      ftp.changeWorkingDirectory(folder);
      LogUtils.log("Changing to directory:[" + folder + "]");
      String realFile = destfolder + File.separator + fileName;
      File localFile = new File(realFile);
      FileOutputStream fos = new FileOutputStream(localFile);
      LogUtils.log("Start downloading..");
      FTPFile[] fs = ftp.listFiles();
      DecimalFormat df = new DecimalFormat("#.00%");
      for (FTPFile f : fs) {
        if (f.getName().equals(fileName)) {
          InputStream is = ftp.retrieveFileStream(f.getName());
          BufferedReader br = new BufferedReader(new InputStreamReader(is));
          String line = br.readLine();
          long transfered = 0, total = f.getSize();
          double ratio = 0.0;
          while (line != null) {
            byte[] buff = line.getBytes();
            transfered += buff.length;
            if (transfered * 1.0 / total - ratio >= 0.01) {
              ratio = transfered * 1.0 / total;
              LogUtils.log("Download size : " + df.format(ratio));
            }
            fos.write(buff);
            line = br.readLine();
          }
          is.close();
          ftp.logout();
          ftp.disconnect();

          LogUtils.log("Download done!");
        }
      }
      fos.close();

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Example #10
0
  public void download(String destPath, FTPClient client) {
    byte[] buffer = new byte[10240];
    try {
      new File(destPath + "/" + sourcePath).getParentFile().mkdirs();
      FileOutputStream out = new FileOutputStream(destPath + "/" + sourcePath);
      InputStream in = client.retrieveFileStream(sourcePath);
      int counter = 0;
      while (true) {
        int bytes = in.read(buffer);
        NeptusLog.pub().info("<###> " + bytes);
        if (bytes < 0) break;

        out.write(buffer, 0, bytes);
        counter += bytes;
        NeptusLog.pub().info("<###> " + counter);
      }
      NeptusLog.pub().info("<###>Finished Transfering");
      out.close();
      in.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Example #11
0
  /**
   * 하나의 파일을 다운로드 한다.
   *
   * @param dir 저장할 경로(서버)
   * @param downloadFileName 다운로드할 파일
   * @param path 저장될 공간
   */
  public void download(String dir, String downloadFileName, String path) {

    FileOutputStream out = null;
    InputStream in = null;
    dir += downloadFileName;
    try {
      in = client.retrieveFileStream(dir);
      out = new FileOutputStream(new File(path));
      int i;
      while ((i = in.read()) != -1) {
        out.write(i);
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      try {
        in.close();
        out.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  };
  @Override
  public Future<Set<File>> download(LayerRequest currentLayer) throws Exception {
    this.currentLayer = currentLayer;
    currentLayer.setMetadata(this.includesMetadata());

    File directory = getDirectory();
    Set<File> fileSet = new HashSet<File>();
    List<String> urls = this.getUrls(currentLayer);
    for (String url : urls) {
      InputStream inputStream = null;
      URL currentURL = new URL(url);
      String ftpServerAddress = currentURL.getHost();
      String currentPath = currentURL.getPath();

      FTPClient ftp = new FTPClient();
      int delimiterIndex = currentPath.lastIndexOf("/");
      String remoteDirectory = currentPath.substring(0, delimiterIndex);
      String fileName = currentPath.substring(delimiterIndex + 1);

      try {
        int reply;
        ftp.connect(ftpServerAddress);
        // Although they are open FTP servers, in order to access the files, a anonymous login is
        // necessary.
        ftp.login("anonymous", "anonymous");
        System.out.println("Connected to " + ftpServerAddress + ".");
        System.out.print(ftp.getReplyString());

        // After connection attempt, you should check the reply code to verify success.
        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
          ftp.disconnect();
          System.err.println("FTP server refused connection.");
          System.exit(1);
        }
        reply = ftp.getReplyCode();

        // enter passive mode
        ftp.enterLocalPassiveMode();

        // change current directory
        ftp.changeWorkingDirectory(remoteDirectory);

        // check if the file with given name exists in ftp server
        try {
          FTPFile[] ftpFiles = ftp.listFiles(fileName);
          if (ftpFiles != null && ftpFiles.length > 0) {
            for (FTPFile file : ftpFiles) {
              if (!file.isFile()) continue;
              System.out.println("Found file:" + file.getName());

              // transfer the file
              inputStream = ftp.retrieveFileStream(file.getName());

              // save the file and add to fileset
              // TODO: ftp file does not contain MIME type.
              File outputFile = OgpFileUtils.createNewFileFromDownload(fileName, "ZIP", directory);
              // FileUtils with a BufferedInputStream seems to be the fastest method with a small
              // sample size.  requires more testing
              BufferedInputStream bufferedIn = null;
              try {
                bufferedIn = new BufferedInputStream(inputStream);
                FileUtils.copyInputStreamToFile(bufferedIn, outputFile);
                fileSet.add(outputFile);
              } finally {
                IOUtils.closeQuietly(bufferedIn);
              }
            }
          }
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          ftp.logout();
        }
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        if (ftp.isConnected()) {
          try {
            ftp.disconnect();
          } catch (IOException ioe) {
            // do nothing
          }
        }
        IOUtils.closeQuietly(inputStream);
      }
    }
    return new AsyncResult<Set<File>>(fileSet);
  };
Example #13
0
 public InputStream retrieveFileStream(String aFileName) throws IOException {
   return ftpClient.retrieveFileStream(aFileName);
 }
Example #14
0
    @Override
    protected Integer doInBackground(Long... params) {
      // Log.w("LOGGER", "Starting...doInBackground");

      try {

        actionBar.setProgressBarVisibility(ProgressBar.VISIBLE);
        con = new FTPClient();
        try {
          String version_actualizar = "";

          // set time out
          con.setConnectTimeout(9000);

          // Log.v("instalacion", "conectando");
          con.connect("62.212.77.173");

          // Log.v("instalacion", "conectado");
          if (con.login("soda", "pepino")) {
            // Log.v("instalacion", "login");
            con.enterLocalPassiveMode(); // important!

            Log.v("instalacion", "changeWorkingDirectory -     " + con.printWorkingDirectory());

            //			  				        con.changeWorkingDirectory("" + "tessdata" + "");

            Log.v("instalacion", "changeWorkingDirectory -     " + con.printWorkingDirectory());

            String[] files = con.listNames();
            FTPFile[] directories = con.listDirectories();
            FTPFile[] archivos = con.listFiles();

            for (int i = 0; i < directories.length; i++) {
              Log.v("instalacion", directories[i].getName().toString());
            }
            for (int i = 0; i < archivos.length; i++) {
              Log.v("instalacion", archivos[i].getName().toString());
            }

            // DESCARGA
            System.out.println("  ---   Remote system is " + con.getSystemName());

            con.setFileType(FTP.BINARY_FILE_TYPE);
            con.enterLocalPassiveMode();

            filename = "tessdata.zip";

            for (int i = 0; i < archivos.length; i++) {
              Log.v("----- instalacion", archivos[i].getName().toString());

              if (archivos[i].getName().toString().equals(filename)) {
                size = archivos[i].getSize();
              }
            }

            try {

              fos =
                  new BufferedOutputStream(
                      new FileOutputStream(
                          Environment.getExternalStorageDirectory().toString() + "/" + filename));
              // Log.i("instalacion"," FileOutputStream ");

              InputStream input = con.retrieveFileStream("tessdata.zip");

              byte data[] = new byte[1024];
              long total = 0;
              int count;
              while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                //				  				                publishProgress((int) (total * 100 / data.length));

                publishProgress(
                    Integer.valueOf((int) (((total * 100 / data.length) * 1000) / size)));
                //				  				                Log.i("instalacion", String.valueOf( (((int) (total *
                // 100 / data.length))*100)/size ));

                //				  				                publishProgress(count);
                fos.write(data, 0, count);
              }

              fos.flush();
              fos.close();

              con.logout();
              con.disconnect();
            } catch (Exception e) {
              // TODO: handle exception
              Log.e(
                  "instalacion",
                  "ERROR al obtener la actualizaci—n de Elara  : " + e.getMessage() + "    ");
              return -4;
            }

          } else {
            // Log.e("instalacion","Nombre usuario o contrase–a ftp no valido.");
            Log.e("instalacion", "Nombre usuario o contrase–a ftp no valido. ");
            return -5;
          }
        } catch (Exception e) {

          Log.e("instalacion", "ERROR de conexion  : " + e.getMessage() + "    ");
          return -2;
        }

        try {
          con.logout();
          return 0;
        } catch (IOException e) {
          // Log.e("instalacion",e.getMessage());
          Log.e("instalacion", "ERROR al desconectar del FTP : " + e.getMessage() + "    ");
          return 0;
        }

      } catch (Exception e) {
        // Log.e("instalacion",e.getMessage());
        Log.e(
            "instalacion", "ERROR al buscar actualizaciones de Elara: " + e.getMessage() + "    ");
        return -1;
      }
    }
Example #15
0
  /**
   * 下载批量文件
   *
   * @param remoteFileNameList
   * @param localFileDirectory
   * @param tempFileDir
   * @param isTextFile
   * @return
   * @throws FtpException
   */
  public boolean getFile(
      ArrayList<String> remoteFileNameList,
      String localFileDirectory,
      String tempFileDir,
      boolean isTextFile)
      throws FtpException {
    // boolean isValidWrt = false;
    byte[] data = new byte[BUFFER];
    int count;
    String localFileName = null;
    String remoteFileName = null;
    String tempLocalFileDir = localFileDirectory;
    File tmpFile = new File(localFileDirectory);
    BufferedInputStream downloadFileIn = null;
    BufferedOutputStream downloadFileOut = null;
    if (!tmpFile.exists()) {
      tmpFile.mkdirs();
    }
    File tempFile = new File(tempFileDir);
    if (!tempFile.exists()) {
      tempFile.mkdirs();
    }
    try {
      // String currPath = ftp.printWorkingDirectory();
      if (isTextFile) {
        ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
      } else {
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
      }
      for (int i = 0; i < remoteFileNameList.size(); i++) {
        tempLocalFileDir = localFileDirectory;
        File temp = new File(remoteFileNameList.get(i));
        localFileName = temp.getName();
        remoteFileName = remoteFileNameList.get(i);
        if (localFileName.indexOf("(") != -1) {
          remoteFileName = remoteFileName.substring(0, remoteFileName.indexOf("("));
          tempLocalFileDir = tempFileDir;
        }
        InputStream ftpReader = ftp.retrieveFileStream(remoteFileName);
        downloadFileIn = new BufferedInputStream(ftpReader, BUFFER);
        downloadFileOut =
            new BufferedOutputStream(
                new FileOutputStream(tempLocalFileDir + File.separator + localFileName), BUFFER);
        while ((count = downloadFileIn.read(data, 0, BUFFER)) != -1) {
          downloadFileOut.write(data, 0, count);
          downloadFileOut.flush();
        }
        ftpReader.close();
        downloadFileIn.close();
        downloadFileOut.close();

        if (!ftp.completePendingCommand()) {
          close();
          throw new FtpException("remote file or local file is not exist!");
        }
        /*
         * isValidWrt = ftp.retrieveFile(remoteFileName, new BufferedOutputStream(new
         * FileOutputStream(tempLocalFileDir + File.separator + localFileName))); if
         * (!isValidWrt) { throw new FtpException("远程文件不存在或本地文件不存在!"); }
         */
      }

    } catch (Exception ex) {
      tmpFile.exists();
      logger.info(
          "\\== ERROR WHILE EXECUTE FTP ==// (File Name:"
              + remoteFileName
              + " ==> "
              + localFileName
              + ") >>>>>>>>>>>>>"
              + ex.getLocalizedMessage());
      close();
      throw new FtpException("ftp file exception:" + ex.getMessage());
    }
    return true;
  }