com.enterprisedt.net.ftp.FTPClient FTP文件上传操作

public class FtpUtil {

    /**
     * 日志记录
     */
    private static Logger logger = LoggerFactory.getLogger(FtpUtil.class);

    /**
     *
     * 功能描述: 连接ftp服务器
     * 
     */
    private static FTPClient connect(FtpServerInfo info) {
        FTPClient ftp = new FTPClient();
        try {
            ftp.setRemoteHost(info.getHost());
            ftp.setRemotePort(info.getPort());
            ftp.setControlEncoding("UTF-8");
            ftp.connect();
            ftp.login(info.getUsername(), info.getPassword());
            ftp.setType(FTPTransferType.BINARY);
        } catch (FTPException e) {
            logger.error("连接 FTP服务器 异常=====" + e.getMessage());
        } catch (IOException e) {
            logger.error("连接 FTP服务器IO异常=====" + e.getMessage());
        }
        return ftp;
    }

        /**
     *
     * 从服务器得到json文件内容
     * 
     */
    public static String getJsonFile(FtpServerInfo info) {
        FTPClient ftp = null;
        try {
            ftp = connect(info);
            if (ftp == null) {
                throw new RuntimeException("ftpClient object is null!");
            }
            String jsonFile = File.separator + info.getRootPath() + File.separator + "file.json";
            if (ftp.existsFile(jsonFile)) {
                byte[] bytes = ftp.get(jsonFile);
                return new String(bytes, Charset.forName("UTF-8"));
            } else {
                return "";
            }
        } catch (Exception e) {
            logger.error("得到json文件异常===============" + e.getMessage());
        } finally {
            try {
                if (ftp != null) {
                    ftp.quit();
                }
            } catch (IOException e1) {
            } catch (FTPException e1) {
            }
        }
    }

    /**
     *
     * 功能描述: 重新上传json文件
     * 
     */
    public static void putJsonFile(FtpServerInfo info, String jsonContent) {
        FTPClient ftp = null;
        try {
            ftp = connect(info);
            if (ftp == null) {
                throw new RuntimeException("ftpClient object is null!");
            }
            String jsonFile = File.separator + info.getRootPath() + File.separator + "file.json";

            ftp.put(jsonContent.getBytes(Charset.forName("UTF-8")), jsonFile);
        } catch (Exception e) {
            logger.error("上传 json文件异常===============" + e.getMessage());
        } finally {
            try {
                if (ftp != null) {
                    ftp.quit();
                }
            } catch (IOException e1) {
            } catch (FTPException e1) {
            }
        }
    }

    /**
     *
     * 功能描述: 根据自提柜代码删除 ftp服务器相应目录下的文件
     * 
     */
    public static void deleteFile(String lockerCode, FtpServerInfo info) {
        FTPClient ftp = null;
        try {
            ftp = connect(info);
            if (ftp == null) {
                throw new RuntimeException("ftpClient object is null!");
            }
            deleteFolder(ftp, File.separator + info.getRootPath() + File.separator + code);
        } catch (Exception e) {
            logger.error("删除文件异常===============" + e.getMessage());
        } finally {
            try {
                if (ftp != null) {
                    ftp.quit();
                }
            } catch (IOException e1) {
            } catch (FTPException e1) {
            }
        }
    }

    /**
     *
     * 功能描述: 上传 文件
     */
    public static void uploadFile(InputStream is, String code, String fileName, FtpServerInfo info) {

        FTPClient ftp = null;
        String rootPath = null;
        String codePath = null;
        try {
            ftp = connect(info);
            if (ftp == null) {
                throw new RuntimeException("ftpClient object is null!");
            }
            // 防止根目录不存在 先建立
            rootPath = File.separator + info.getRootPath();
            if (!ftp.existsDirectory(rootPath)) {
                ftp.mkdir(rootPath);
            }
            // 若存在 则先删除对应的目录
            codePath = rootPath + File.separator + code;
            if (ftp.existsDirectory(lockerCodePath)) {
                deleteFolder(ftp, codePath);
            }
            // 新建目录
            ftp.mkdir(codePath);
            // 上传文件
            ftp.put(is, codePath+ File.separator + fileName);

        } catch (IOException e) {
            logger.error("上传文件至  FTP服务器连接 异常=====" + e.getMessage());
        } catch (FTPException e) {
            logger.error("上传文件至  FTP服务器连接 异常=====" + e.getMessage());
        } finally {
            try {
                if (ftp != null) {
                    ftp.quit();
                }
            } catch (IOException e1) {
            } catch (FTPException e1) {
            }
        }
    }

    /**
     *
     * 功能描述:递归删除目录下文件及子文件夹
     * 
     */
    private static void deleteFolder(FTPClient ftpClient, String dirPath) {

        try {
            // 切换到根目录
            ftpClient.chdir(File.separator);
            FTPFile[] ftpFiles = ftpClient.dirDetails(dirPath);
            if (ftpFiles == null) {
                return;
            }
            for (FTPFile file : ftpFiles) {
                if (file.isDir()) {
                    deleteFolder(ftpClient, dirPath + File.separator + file.getName());
                }
                if (file.isFile()) {
                    ftpClient.delete(dirPath + File.separator + file.getName());
                }
            }
            // 回到 父目录 删除
            ftpClient.cdup();
            ftpClient.rmdir(dirPath);

        } catch (IOException e) {
        } catch (FTPException e) {
        } catch (ParseException e) {
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值