• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >ruby > ruby ftp封装实例详解

ruby ftp封装实例详解

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了ruby实例,ruby 实例变量,ruby,ruby碧玺,ruby woo口红等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

 ruby ftp封装实例详解

最近自己用ruby 封装了一个Net::FTP的工具类.

class FtpTool

 def initialize()

  @current_ftp = create_ftp

 end

 

</div>

  # 获取指定格式的文件名称列表

  # 例如: source = "test/*.txt"

  # 返回: [source/file_name.txt]

 def fetch_remote_filenames(source)

  return [] if source.blank?

  log_info("source is " + source)

  filenames = @current_ftp.nlst(source)

  filenames

 end

</div>

   # 获取服务器上确切名称的文件

  # 例如: get("test/test.txt")

  # 文件将被保存到本地 tmp/test/test.txt

 def get(origin_file)

  local_file = local_file(origin_file)

  local_file.gsub("\\", "\\\\") #此处注意是window下执行, 在linux下需要注意改成/

  log_info("Ftp Get: #{origin_file} -> #{local_file}")

  begin

   @current_ftp.getbinaryfile(origin_file, local_file+".tmp")

  rescue

   delete_local_file(local_file+".tmp")

  end

  rename_local_file(local_file+".tmp", local_file) if File.exist?(local_file+".tmp")

 end

</div>

   # 上传文件到指定的路径

  # 例如: put("tmp\\test\\test.txt", "/test/")

def put(origin_file, remote_path)

  return nil if not File.exist?(origin_file)

  _file_name = File.basename(origin_file)

  _root = @current_ftp.getdir

  @current_ftp.chdir(remote_path)

  log_info("Ftp put: #{origin_file} -> #{remote_path}")

  begin

   @current_ftp.putbinaryfile(origin_file, remote_path + _file_name + ".tmp")

  rescue

   delete(remote_path + _file_name + ".tmp")

  end

  @current_ftp.chdir(_root)

  rename(remote_path + _file_name + ".tmp", remote_path + _file_name)

 end

 

</div>

  # 关闭ftp

 def close

  @current_ftp.close if @current_ftp

 end

 

</div>

  # 服务器copy文件 

 def copy(origin_file, file_path)

  local_file = local_file(origin_file)

  _file_name = File.basename(origin_file)

  begin

</div>

      #1. 到本地    

 log_info("FTP get file to:" + local_file+".tmp")

   @current_ftp.getbinaryfile(origin_file, local_file+".tmp")

   return nil if not File.exist?(local_file+".tmp")

</div>

      #2. 到服务器   

  log_info("FTP put file to :" + file_path + _file_name + ".tmp")

   @current_ftp.putbinaryfile(local_file+".tmp", file_path + _file_name + ".tmp")

   #3. 改名字

   rename(file_path + _file_name + ".tmp", file_path + _file_name)

   #5. 删除本地

   delete_local_file(local_file + ".tmp")

  rescue => e

   log_info(e)

   #4. 删除服务器上临时文件

   delete(file_path + origin_file + ".tmp")

   #5. 删除本地

   delete_local_file(local_file + ".tmp")

  end

 end

</div>

   # 服务器上移动文件

 def move(origin_file, file_path)

  _file_name = File.basename(origin_file)

  begin

   copy(origin_file, file_path)

   # 删除服务器上源文件

   delete(origin_file)

  rescue => e

   log_info(e)

   # 删除临时文件,如果存在

   delete(file_path + _file_name + ".tmp")

   # 删除服务器上目标文件, 如果存在

   delete(file_path + _file_name)

  end

 end

</div>

   # 重命名服务器文件

 def rename(origin_file, file)

  if not @current_ftp.list(origin_file).blank?

   log_info("FTP rename #{origin_file} to #{file}")

   @current_ftp.rename(origin_file, file)

  end

 end

 

</div>

  # 删除服务器上的文件

 def delete(origin_file)

  if not @current_ftp.list(origin_file).blank?

   log_info("FTP delete #{origin_file}")

   @current_ftp.delete(origin_file)

  end

 end

 
</div>

 # ftp 是否关闭

 def closed?

  @current_ftp.closed?

 end

 

 class << self
</div>

    # 文件编码转换

 def convert(src_file, dest_file, from_encode, to_encode )

   log_info("Convert #{src_file} to #{dest_file}")

   cd = Iconv.new(to_encode, from_encode)

   File.open(dest_file, "w") do |out|

    File.open(src_file) do |in_stream|

     in_stream.each_line do |line|

      begin

       new_line = cd.iconv(line)

       out.write(new_line)

      rescue => e

       log_info "convert line error : #{line}"

       next

      end

     end

    end

   end

   cd.close

   dest_file

  end

 end

 

 protected
</div>

  #生成ftp

 def create_ftp

  require "net/ftp"

  ftp = Net::FTP.new

  ftp.connect(ftp_host, ftp_port)

  ftp.login(ftp_user, ftp_pwd)

  ftp.passive = ftp_mode

  ftp

 end

 

</div>

  #本地路径

def local_file(file)

  local = File.join("tmp/", file)

  FileUtils.makedirs(File.dirname(local))

  local

 end

 

</div>

  # 删除本地文件 

 def delete_local_file(file)

  if File.exist?(file)

   log_info("delete local file : " + file)

   File.delete(file)

  end

 end

 

</div>

  # 重命名本地文件

 def rename_local_file(origin_file, file)

  if File.exist?(origin_file)

   log_info("rename local file : " + origin_file + " to " + file)

   File.rename(origin_file, file)

  end

 end

 

</div>

  #初始化参数

 def ftp_host; "x.x.x.x" end

 def ftp_port; "21" end

 def ftp_user; "x" end

 def ftp_pwd ; "x" end

 def ftp_mode; true end

end
</div>

 

</div>

您可能想查找下面的文章:

  • Ruby中访问SQL Server数据库的配置实例
  • Ruby使用C++扩展实例(含C++扩展代码示例)
  • ruby元编程实际使用实例
  • 优化Ruby脚本效率实例分享
  • Ruby中的反射(Reflection)应用实例
  • Ruby进行文件信息输出实例代码
  • ruby ftp封装实例详解
  • Ruby中钩子方法的运用实例解析
  • 实例讲解Ruby中的钩子方法及对方法调用添加钩子
  • 实例解析Ruby程序中调用REXML来解析XML格式数据的用法

相关文章

  • Ruby升级后no such file to load -- readline解决办法
  • 使用Ruby来处理文本的教程
  • Ruby简洁学习笔记(二):类继承、属性、类变量
  • ruby 存取器 概念
  • RVM安装和使用总结笔记
  • GitHub倡导的Ruby代码编写风格总结
  • Ruby中的迭代器详解
  • Ruby on Rails实现最基本的用户注册和登录功能的教程
  • 使用Ruby程序实现web信息抓取的教程
  • Ruby 之 class 中的 private、 protected、public

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • mac os gem安装json出现error failed的解决办法
    • Ruby中区分运行来源的方法
    • 设计模式中的观察者模式在Ruby编程中的运用实例解析
    • 艰难完成 nginx + puma 部署 rails 4的详细记录
    • ruby 杂项
    • 在Mac OS X下安装Ruby运行环境的详细步骤
    • Ruby基本的环境变量设置以及常用解释器命令介绍
    • 优化Ruby脚本效率实例分享
    • 使用Ruby来编写访问Twitter的命令行应用程序的教程
    • 详解Ruby中范围的概念

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有