Java Code Examples for org.apache.commons.net.ftp.FTPFile#isFile()

The following examples show how to use org.apache.commons.net.ftp.FTPFile#isFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FTPUtil.java    From anyline with Apache License 2.0 5 votes vote down vote up
public List<String> files(String dir) throws IOException {   
    List<String> fileList = new ArrayList<String>();   
   
    FTPFile[] ftpFiles = client.listFiles(dir);   
    for (int i = 0; ftpFiles!=null && i<ftpFiles.length; i++) {   
        FTPFile ftpFile = ftpFiles[i];   
        if (ftpFile.isFile()) {   
            fileList.add(ftpFile.getName());   
        }   
    }   
       
    return fileList;   
}
 
Example 2
Source File: FtpServiceImpl.java    From zfile with MIT License 5 votes vote down vote up
@Override
public List<FileItemDTO> fileList(String path) {
    ftp.reconnectIfTimeout();
    String fullPath = StringUtils.getFullPath(basePath, path);
    ftp.cd(fullPath);
    FTPFile[] ftpFiles = new FTPFile[]{};
    try {
        ftp.getClient().changeWorkingDirectory("/");
        ftpFiles = ftp.getClient().listFiles(fullPath);
    } catch (Exception e) {
        e.printStackTrace();
        // ignore
    }

    List<FileItemDTO> fileItemList = new ArrayList<>();

    for (FTPFile ftpFile : ftpFiles) {
        FileItemDTO fileItemDTO = new FileItemDTO();
        fileItemDTO.setName(ftpFile.getName());
        fileItemDTO.setSize(ftpFile.getSize());
        fileItemDTO.setTime(ftpFile.getTimestamp().getTime());
        fileItemDTO.setType(ftpFile.isDirectory() ? FileTypeEnum.FOLDER : FileTypeEnum.FILE);
        fileItemDTO.setPath(path);
        if (ftpFile.isFile()) {
            fileItemDTO.setUrl(getDownloadUrl(StringUtils.concatUrl(path, fileItemDTO.getName())));
        }
        fileItemList.add(fileItemDTO);
    }
    return fileItemList;
}
 
Example 3
Source File: FileNamePrefixFilter.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(FTPFile ftpFile) {

    if (prefixList.isEmpty())
        return ftpFile.isFile();

    return (ftpFile.isFile() && prefixList.contains(FilenameUtils.getPrefix(ftpFile.getName())));
}
 
Example 4
Source File: FileNameEqualFilter.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(FTPFile ftpFile) {

    if (fullFileName.isEmpty())
        return ftpFile.isFile();

    return (ftpFile.isFile() && ftpFile.getName().equals(fullFileName));
}
 
Example 5
Source File: FileNameSuffixFilter.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(FTPFile ftpFile) {

    if (suffixList.isEmpty())
        return ftpFile.isFile();

    return (ftpFile.isFile() && suffixList.contains(FilenameUtils.getExtension(ftpFile.getName())));
}
 
Example 6
Source File: FtpResponse.java    From anthelion with Apache License 2.0 5 votes vote down vote up
private byte[] list2html(List list, String path, boolean includeDotDot) {

    //StringBuffer x = new StringBuffer("<!doctype html public \"-//ietf//dtd html//en\"><html><head>");
    StringBuffer x = new StringBuffer("<html><head>");
    x.append("<title>Index of "+path+"</title></head>\n");
    x.append("<body><h1>Index of "+path+"</h1><pre>\n");

    if (includeDotDot) {
      x.append("<a href='../'>../</a>\t-\t-\t-\n");
    }

    for (int i=0; i<list.size(); i++) {
      FTPFile f = (FTPFile) list.get(i);
      String name = f.getName();
      String time = HttpDateFormat.toString(f.getTimestamp());
      if (f.isDirectory()) {
        // some ftp server LIST "." and "..", we skip them here
        if (name.equals(".") || name.equals(".."))
          continue;
        x.append("<a href='"+name+"/"+"'>"+name+"/</a>\t");
        x.append(time+"\t-\n");
      } else if (f.isFile()) {
        x.append("<a href='"+name+    "'>"+name+"</a>\t");
        x.append(time+"\t"+f.getSize()+"\n");
      } else {
        // ignore isSymbolicLink()
        // ignore isUnknown()
      }
    }

    x.append("</pre></body></html>\n");

    return new String(x).getBytes();
  }
 
Example 7
Source File: FtpResponse.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
private byte[] list2html(List<FTPFile> list, String path, boolean includeDotDot) {

    //StringBuffer x = new StringBuffer("<!doctype html public \"-//ietf//dtd html//en\"><html><head>");
    StringBuffer x = new StringBuffer("<html><head>");
    x.append("<title>Index of "+path+"</title></head>\n");
    x.append("<body><h1>Index of "+path+"</h1><pre>\n");

    if (includeDotDot) {
      x.append("<a href='../'>../</a>\t-\t-\t-\n");
    }

    for (int i=0; i<list.size(); i++) {
      FTPFile f = (FTPFile) list.get(i);
      String name = f.getName();
      String time = HttpDateFormat.toString(f.getTimestamp());
      if (f.isDirectory()) {
        // some ftp server LIST "." and "..", we skip them here
        if (name.equals(".") || name.equals(".."))
          continue;
        x.append("<a href='"+name+"/"+"'>"+name+"/</a>\t");
        x.append(time+"\t-\n");
      } else if (f.isFile()) {
        x.append("<a href='"+name+    "'>"+name+"</a>\t");
        x.append(time+"\t"+f.getSize()+"\n");
      } else {
        // ignore isSymbolicLink()
        // ignore isUnknown()
      }
    }

    x.append("</pre></body></html>\n");

    return new String(x).getBytes();
  }
 
Example 8
Source File: FtpClientService.java    From WeEvent with Apache License 2.0 4 votes vote down vote up
public void downLoadFile(String remoteFilePath, String localPath) throws BrokerException {
    if (!this.ftpClient.isConnected() || !this.ftpClient.isAvailable()) {
        log.error("ftp client not connected to a server");
        throw new BrokerException(ErrorCode.FTP_CLIENT_NOT_CONNECT_TO_SERVER);
    }
    if (StringUtils.isBlank(remoteFilePath)) {
        log.error("remote file path invalid, {}", remoteFilePath);
        throw new BrokerException(ErrorCode.FTP_INVALID_REMOTE_PATH);
    }
    if (StringUtils.isBlank(localPath)) {
        log.error("local file path invalid, {}", localPath);
        throw new BrokerException(ErrorCode.FTP_NOT_EXIST_PATH);
    }

    try {
        this.ftpClient.enterLocalPassiveMode();
        this.ftpClient.setDataTimeout(300 * 1000);
        this.ftpClient.enterLocalPassiveMode();
        this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        // when upload directory, and change working directory to default failed, relative directory is illegal.
        boolean changeDir = this.ftpClient.changeWorkingDirectory(this.defaultDir);
        if (!changeDir) {
            log.error("change working directory failed, {}", this.defaultDir);
            throw new BrokerException(ErrorCode.FTP_CHANGE_WORKING_DIR_FAILED);
        }

        FTPFile[] ftpFiles = ftpClient.listFiles(remoteFilePath);
        FTPFile ftpFile = null;
        if (ftpFiles.length != 0) {
            ftpFile = ftpFiles[0];
        }

        if (ftpFile != null && ftpFile.isFile()) {
            // create local file path(remoteFilePath maybe a multi-layer directory)
            File localFile = new File(localPath, remoteFilePath);
            if (!localFile.getParentFile().exists()) {
                boolean retMkdir = localFile.getParentFile().mkdirs();
                if (!retMkdir) {
                    log.error("make local parent dir failed, {}", localFile.getParentFile().getName());
                    throw new BrokerException(ErrorCode.FTP_MAKE_DIR_FAILED);
                }
            }

            // change ftpClient work path
            OutputStream outputStream = new FileOutputStream(localFile);
            String workDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf(PATH_SEPARATOR));
            if (StringUtils.isBlank(workDir)) {
                workDir = PATH_SEPARATOR;
            }

            changeDir = ftpClient.changeWorkingDirectory(workDir);
            if (!changeDir) {
                log.error("change working directory failed, {}", workDir);
                throw new BrokerException(ErrorCode.FTP_CHANGE_WORKING_DIR_FAILED);
            }
            boolean retrieve = ftpClient.retrieveFile(ftpFile.getName(), outputStream);
            if (!retrieve) {
                log.error("retrieve file from ftp server failed, {}", ftpFile.getName());
                throw new BrokerException(ErrorCode.FTP_RETRIEVE_FILE_FAILED);
            }

            outputStream.flush();
            outputStream.close();
            log.info("file download success, {}", ftpFile.getName());
        } else {
            log.error("remote file name error, {}", remoteFilePath);
            throw new BrokerException(ErrorCode.FTP_UNKNOWN_REMOTE_FILE);
        }
    } catch (IOException e) {
        throw new BrokerException(e.getMessage());
    }
}