• java 获取服务器(ftp)指定文件夹内的文件


    package common.util;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;

    import javax.servlet.http.HttpServletResponse;

    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    import org.apache.commons.net.PrintCommandListener;
    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPFile;
    import org.apache.commons.net.ftp.FTPReply;
    import org.apache.log4j.Logger;

    /**
    * 列出FTP服务器上指定目录下面的所有文件
    */
    public class FTPListAllFiles {
    private static Logger logger = Logger.getLogger(FTPListAllFiles.class);
    public FTPClient ftp;
    public ArrayList<String> arFiles;
    private static final String FAR_SERVER_URL="***.***.***.***";
    private static final int SERVER_PORT=21;
    private static final String SERVER_USER="***";
    private static final String SERVER_PWD="***";
    private static final String path="/123/456/";
    private static final String showPath="";
     

    /**
    * 登陆FTP服务器
    * @param host FTPServer IP地址
    * @param port FTPServer 端口
    * @param username FTPServer 登陆用户名
    * @param password FTPServer 登陆密码
    * @return 是否登录成功
    * @throws IOException
    */
    public boolean login(String host,int port,String username,String password) throws IOException{
    this.ftp.connect(host,port);
    if(FTPReply.isPositiveCompletion(this.ftp.getReplyCode())){
    if(this.ftp.login(username, password)){
    this.ftp.setControlEncoding("GBK");
    return true;
    }
    }
    if(this.ftp.isConnected()){
    this.ftp.disconnect();
    }
    return false;
    }

    /**
    * 关闭数据链接
    * @throws IOException
    */
    public void disConnection() throws IOException{
    if(this.ftp.isConnected()){
    this.ftp.disconnect();
    }
    }

    /**
    * 递归遍历出目录下面所有文件
    * @param pathName 需要遍历的目录,必须以"/"开始和结束
    * @throws IOException
    */
    public String List(String pathName) throws IOException{

    StringBuffer filename=new StringBuffer();
    if(pathName.startsWith("/")&&pathName.endsWith("/")){
    String directory = pathName;
    //更换目录到当前目录
    this.ftp.changeWorkingDirectory(directory);
    ftp.enterLocalPassiveMode();
    FTPFile[] files = this.ftp.listFiles();
      if(files!=null){
        for (int i = 0; i < files.length; i++) {
          if(files[i].isFile()){
            String n=new String(files[i].getName().getBytes("gbk"),"utf-8");
            if(i==files.length-1){
            filename.append(n+","+showPath);
           }else{
              filename.append(n+",");
             }

          }
        }
       }
      }
        return filename.toString();
    }

    //获取指定文件夹内的文件名称
    public static String getFilenames(){
      String names="";
      FTPListAllFiles f = new FTPListAllFiles();
      try {
         if(f.login(FAR_SERVER_URL, SERVER_PORT, SERVER_USER, SERVER_PWD)){
          names= f.List(path);
         }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{

            try {
                f.disConnection();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
            }

          return names;
      }
    //测试
    /* public static void main(String[] args) throws IOException {
      System.out.println(getFilenames());
    } */
    }

      注意:红色标记的代码很重要,如果Linux搭建的ftp环境没有开启被动模式传输数据时,一般不会读取到指定文件夹内的文件名称。

    经过在网上查找相关的知识,该语句的作用为:

        调用FTPClient.enterLocalPassiveMode();这个方法的意思就是每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据。为什么要这样做呢,因为ftp server可能每次开启不同的端口来传输数据,但是在linux上,由于安全限制,可能某些端口没有开启,所以就出现阻塞。

  • 相关阅读:
    宝塔面板连接数据库失败
    fastadmin上线部署中遇到访问路径问题
    宝塔部署时,出现“open_basedir restriction in effect”错误
    layui hint:upload is not a valid module
    thinkphp--控制器怎么分配变量到公共模板
    jquey click事件无效
    1.31 SVN代码版本控制
    8.1 性能优化简介
    5.31 Nginx最全面知识
    4.115 Spring的事务管理
  • 原文地址:https://www.cnblogs.com/hm1990hpu/p/8143388.html
Copyright © 2020-2023  润新知