zhangjs1的gravatar头像
zhangjs1 2014-12-21 15:13:37

java操作FTP实现文件上传下载删除操作

/**
     * 获取ftp上文件的最后修改时间
     * @param host     ftp主机地址 e.g. "127.0.0.1"
     * @param username 用户名      e.g. "username"
     * @param password 密码        e.g. "passowrd"
     * @param path     文件路径    e.g. "ftputil/test.txt"
     * @return Date 如果连接成功返回java.util.Date时间,如果连接失败返回null
     * @throws IOException
     * @throws SocketException
     */
    public Date getFileLastModifiedTime(String host,String username,String password,String path) throws SocketException, IOException{
        Date lastModifiedDate = null;
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(host);
        boolean isLogin = ftpClient.login(username, password);
        //连接ftp失败返回null
        if(!isLogin){
            ftpClient.disconnect();
            ftpClient = null;
            return null;
        }
        //获取ftp上path路径下的文件
        FTPFile[] fileList = ftpClient.listFiles(path);
        for (int i = 0; i < fileList.length; i++) {
            lastModifiedDate = fileList[i].getTimestamp().getTime();
        }
        return lastModifiedDate;
    }
    
    /**
     * 获得ftp路径下面所有文件的修改时间
     * @param host     ftp主机地址 e.g. "127.0.0.1"
     * @param username 用户名      e.g. "username"
     * @param password 密码        e.g. "passowrd"
     * @param path     文件路径    e.g. "ftputil"
     * @return java.util.Map 连接成功返回一个key是文件名,value是最后修改时间(java.util.Date)的一个map;连接失败返回null
     * @throws SocketException
     * @throws IOException
     */
    public Map getAllFileLastModifiedTime(String host,String username,String password,String path) throws SocketException, IOException{
        Date lastModifiedDate = null;
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(host);
        boolean isLogin = ftpClient.login(username, password);
        //连接ftp失败返回null
        if(!isLogin){
            ftpClient.disconnect();
            ftpClient = null;
            return null;
        }
        //获取ftp上path路径下的文件
        FTPFile[] fileList = ftpClient.listFiles(path);
        //HashMap容量:fileList.length
        Map map = new HashMap(fileList.length);
        for (int i = 0; i < fileList.length; i++) {
            lastModifiedDate = fileList[i].getTimestamp().getTime();
            //key 文件名;value修改时间(java.util.Date)
            map.put(fileList[i].getName(), lastModifiedDate);
        }
        return map;
    }

打赏

文件名:ftp.zip,文件大小:1.386K 下载
  • /
    • /ftp.txt
最代码最近下载分享源代码列表最近下载
方肖建  LV3 2021年9月2日
业务管理  LV7 2021年5月1日
一蓑烟雨  LV11 2021年4月2日
dpj123  LV1 2020年4月21日
ncd12320  LV8 2020年1月12日
不想起名字aaa  LV1 2019年12月27日
luohaipeng  LV23 2019年12月3日
1358849392  LV21 2019年10月25日
unique11  LV3 2019年10月18日
qiheideguang  LV16 2019年9月25日
最代码最近浏览分享源代码列表最近浏览
WBelong  LV7 2023年12月19日
浪里格朗  LV4 2023年1月31日
lcy123ww  LV5 2022年11月16日
wuyu8995861  LV7 2022年8月8日
newhaijun  LV15 2022年7月20日
le10000  LV2 2022年7月4日
疯子庭  LV8 2022年5月31日
xueqianxun  LV1 2022年3月25日
十月*  LV1 2021年10月11日
sosloser 2021年9月22日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友