• 检验指定路径的文件是否存在ftp服务器中


    import org.apache.commons.net.ftp.FTP;
    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPReply;

    /**
    *  检验指定路径的文件是否存在ftp服务器中
    * @param filePath--指定绝对路径的文件
    * @param user--ftp服务器登陆用户名
    * @param passward--ftp服务器登陆密码
    * @param ip--ftp的IP地址
    * @param port--ftp的端口号
    * @return
    */

    public static boolean isFTPFileExist(String filePath,String user,String passward,String ip,int port){
            FTPClient ftp = new FTPClient();
            try {
        // 连接ftp服务器
                ftp.connect(ip, port);
                // 登陆    
                ftp.login(user, passward);
               // 检验登陆操作的返回码是否正确
                if(!FTPReply.isPositiveCompletion(ftp.getReplyCode())){
                    ftp.disconnect();
                    return false;
                }
                
                ftp.enterLocalActiveMode();
                // 设置文件类型为二进制,与ASCII有区别
                ftp.setFileType(FTP.BINARY_FILE_TYPE);
                // 设置编码格式
                ftp.setControlEncoding("GBK");
                
                // 提取绝对地址的目录以及文件名
                filePath = filePath.replace("ftp://"+ip+":"+port+"/", "");
                String dir = filePath.substring(0, filePath.lastIndexOf("/"));
                String file = filePath.substring(filePath.lastIndexOf("/")+1);
                
                // 进入文件所在目录,注意编码格式,以能够正确识别中文目录
                ftp.changeWorkingDirectory(new String(dir.getBytes("GBK"),FTP.DEFAULT_CONTROL_ENCODING));

                // 检验文件是否存在
                InputStream is = ftp.retrieveFileStream(new String(file.getBytes("GBK"),FTP.DEFAULT_CONTROL_ENCODING));
                if(is == null || ftp.getReplyCode() == FTPReply.FILE_UNAVAILABLE){
                    return false;
                }
                
                if(is != null){
                    is.close();
                    ftp.completePendingCommand();
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(ftp != null){
                    try {
                        ftp.disconnect();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return false;
    }

  • 相关阅读:
    出现org.apache.ibatis.binding.BindingException异常
    EasyExcel读写操作
    window下运行nginx出现nginx: [emerg] bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
    vue Module build failed: Error: Missing binding E:vuevue-demo ode_modules ode-sa ssvendorwin64
    Axios谷粒学院学习
    springboot中数据库的连接
    多表删除,删除一个表的同时删除中间表
    今天写了一个SSM小项目,运行之后,前端页面的CSS、js样式显示不出来,具体操作如下:
    Java中Iterator(迭代器)实现原理
    写一些东西,记录一下成长的过程
  • 原文地址:https://www.cnblogs.com/zhangtan/p/8489031.html
Copyright © 2020-2023  润新知