本文共 2815 字,大约阅读时间需要 9 分钟。
/**
* 从FTP中下载文件至本地路径 remoteFileName FTP服务器上文件名称 localFileName 本地文件名 * * @return */ private boolean getFileFromFtp(String remoteFileName, String localFileName) throws Exception {boolean flag = false;
FtpConfig ftpConfig = new FtpConfig();String location = "";
String password = ""; int port = 0; String server = ""; String userName = "";if (StringUtils.isBlank(location)) {
if (SpringContextHolder.getApplicationContext().getEnvironment() .acceptsProfiles(ProfileCode.PROD.name())) { location = ConfKit.getApp("ftp_location"); } else if (SpringContextHolder.getApplicationContext() .getEnvironment().acceptsProfiles(ProfileCode.QA.name())) { location = ConfKit.getAppQA("ftp_location"); } else { location = ConfKit.getAppQA("ftp_location"); } }if (StringUtils.isBlank(password)) {
if (SpringContextHolder.getApplicationContext().getEnvironment() .acceptsProfiles(ProfileCode.PROD.name())) { password = ConfKit.getApp("ftp_password"); } else if (SpringContextHolder.getApplicationContext() .getEnvironment().acceptsProfiles(ProfileCode.QA.name())) { password = ConfKit.getAppQA("ftp_password"); } else { password = ConfKit.getAppQA("ftp_password"); } }if (port == 0) {
if (SpringContextHolder.getApplicationContext().getEnvironment() .acceptsProfiles(ProfileCode.PROD.name())) { port = Integer.valueOf(ConfKit.getApp("ftp_port")); } else if (SpringContextHolder.getApplicationContext() .getEnvironment().acceptsProfiles(ProfileCode.QA.name())) { port = Integer.valueOf(ConfKit.getAppQA("ftp_port")); } else { port = Integer.valueOf(ConfKit.getAppQA("ftp_port")); } }if (StringUtils.isBlank(server)) {
if (SpringContextHolder.getApplicationContext().getEnvironment() .acceptsProfiles(ProfileCode.PROD.name())) { server = ConfKit.getApp("ftp_server"); } else if (SpringContextHolder.getApplicationContext() .getEnvironment().acceptsProfiles(ProfileCode.QA.name())) { server = ConfKit.getAppQA("ftp_server"); } else { server = ConfKit.getAppQA("ftp_server"); } }if (StringUtils.isBlank(userName)) {
if (SpringContextHolder.getApplicationContext().getEnvironment() .acceptsProfiles(ProfileCode.PROD.name())) { userName = ConfKit.getApp("ftp_userName"); } else if (SpringContextHolder.getApplicationContext() .getEnvironment().acceptsProfiles(ProfileCode.QA.name())) { userName = ConfKit.getAppQA("ftp_userName"); } else { userName = ConfKit.getAppQA("ftp_userName"); } } /* 从上面读的基本配置信息,具体例子如下location = "/deve";
password = "PWD"; port = 21; server = "IP"; userName = "samsungshare";*/ftpConfig.setLocation(location); ftpConfig.setPassword(password); ftpConfig.setPort(port); ftpConfig.setServer(server); ftpConfig.setUsername(userName); FtpKit ftpKit = new FtpKit(); ftpKit.connectServer(ftpConfig); // 将远程FTP中文件下载到本地目录中 flag = ftpKit.download(remoteFileName, localFileName); ftpKit.closeServer(); return flag; }
转载地址:http://qwtml.baihongyu.com/