從ftp上傳下載檔案(一)

flzhang發表於2016-05-16

/**
  * 從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部落格 ” ,連結:http://blog.itpub.net/750077/viewspace-2100610/,如需轉載,請註明出處,否則將追究法律責任。

相關文章