ITPub博客

首页 > 应用开发 > Java > 从ftp上传下载文件(一)

从ftp上传下载文件(一)

原创 Java 作者:flzhang 时间:2016-05-16 10:45:28 0 删除 编辑

/**
  * 从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;
 }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/750077/viewspace-2100610/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-03-26

  • 博文量
    97
  • 访问量
    773862