java如何查看远程服务器下文件夹下指定的文件

2025-10-26 13:50:21

1、/** * 登陆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; } 

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

3、/** * 递归遍历出目录下面所有文件 * @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();}

4、//获取指定文件夹内的文件名称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;  }

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

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢