查看: 3072|回复: 5

[通用发布] 在RM中访问FTP(上传/下载文件)

[复制链接]
梦石
0
星屑
3069
在线时间
1054 小时
注册时间
2011-8-16
回帖
223
发表于 2016-7-11 23:41:54 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
这个脚本对一系列用于访问FTP的Windows API 进行了简单的封装。仅供测试,不建议用于游戏中(一般游戏也不需要用到这个)。
脚本很简陋,没有异常处理,而且上传、下载文件名中含中文的文件会导致乱码。(应该是编码的问题,如果有人知道解决方法还请指教。)

脚本:
[pre lang="ruby"]module Net
  #需要用到的部分常数
  AGENT = "RGSS"  #调用WinINet函数的应用程序
  INTERNET_OPEN_TYPE_PRECONFIG = 0  #使用IE中的连接设置
  INTERNET_SERVICE_FTP = 1  #指定服务类型为FTP
  INTERNET_DEFAULT_FTP_PORT = 21  #FTP服务器的默认端口
  #需要用到的API函数
  IO = Win32API.new('wininet','InternetOpen','plppl','l')
  IC = Win32API.new('wininet','InternetConnect','lplpplll','l')
  ICH = Win32API.new('wininet','InternetCloseHandle','l','i')
  FCD = Win32API.new('wininet','FtpCreateDirectory','lp','i')
  FDF = Win32API.new('wininet','FtpDeleteFile','lp','i')
  FGCD = Win32API.new('wininet','FtpGetCurrentDirectory','lpp','i')
  FGF = Win32API.new('wininet','FtpGetFile','lppilll','i')
  FPF = Win32API.new('wininet','FtpPutFile','lppll','i')
  FRD = Win32API.new('wininet','FtpRemoveDirectory','lp','i')
  FRF = Win32API.new('wininet','FtpRenameFile','lpp','i')
  FSCD = Win32API.new('wininet','FtpSetCurrentDirectory','lp','i')

end

class FTP
  include Net
  #--------------------------------------------------------------------------
  # ● 初始化
  #--------------------------------------------------------------------------
  def initialize(user,passwd,server,port = INTERNET_DEFAULT_FTP_PORT)
    #初始化
    @h_internet = IO.call(AGENT,INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0)
    #建立FTP Session
    @h_connect = IC.call(@h_internet,server,port,user,passwd,INTERNET_SERVICE_FTP,0,0)
  end
  #--------------------------------------------------------------------------
  # ● FtpCreateDirectory 新建文件夹
  #     dir : 新文件夹名
  #--------------------------------------------------------------------------
  def mkdir(dir)
    FCD.call(@h_connect,dir)
  end
  #--------------------------------------------------------------------------
  # ● FtpDeleteFile 删除服务器上的文件
  #     file : 目标文件名
  #--------------------------------------------------------------------------
  def delete(file)
    FDF.call(@h_connect,file)
  end
  #--------------------------------------------------------------------------
  # ● FtpGetCurrentDirectory 获取当前目录
  #--------------------------------------------------------------------------
  def pwd
    path = "\0" * 250
    FGCD.call(@h_connect,path,250)
    path.delete("\0")
  end
  #--------------------------------------------------------------------------
  # ● FtpGetFile 文件下载
  #     remote_file : 远程文件名
  #     new_file : 下载到本地后的文件名
  #     fail_if_exists : 是否覆盖同名文件(为0时覆盖,为1时下载失败),默认为0
  #     attr : 下载后文件的属性(默认为0x80,即FILE_ATTRIBUTE_NORMAL)
  #     type : 指定传输方式(1 = ASCII, 2 = BINARY),默认为2
  #--------------------------------------------------------------------------
  def get_file(remote_file,new_file,fail_if_exists = 0,attr = 0x80,type = 2)
    FGF.call(@h_connect,remote_file,new_file,fail_if_exists,attr,type,0)
  end
  #--------------------------------------------------------------------------
  # ● FtpPutFile 上传文件至服务器
  #     local_file : 本地待上传文件名
  #     new_remote_file : 上传到服务器后的文件名
  #     type : 指定传输方式(1 = ASCII, 2 = BINARY),默认为2
  #--------------------------------------------------------------------------
  def put_file(local_file,new_remote_file,type = 2)
    FPF.call(@h_connect,local_file,new_remote_file,type,0)
  end
  #--------------------------------------------------------------------------
  # ● FtpRemoveDirectory 删除服务器上的文件夹
  #     dir : 目标文件夹名
  #--------------------------------------------------------------------------
  def rmdir(dir)
    FRD.call(@h_connect,dir)
  end
  #--------------------------------------------------------------------------
  # ● FtpRenameFile 重命名服务器上的文件
  #     existing : 目标文件名
  #     new_name : 新文件名
  #--------------------------------------------------------------------------
  def rename(existing,new_name)
    FRF.call(@h_connect,existing,new_name)
  end
  #--------------------------------------------------------------------------
  # ● FtpSetCurrentDirectory 更改当前目录
  #     dir : 新路径
  #--------------------------------------------------------------------------
  def chdir(dir)
    FSCD.call(@h_connect,dir)
  end

  #--------------------------------------------------------------------------
  # ● InternetCloseHandle 关闭连接
  #--------------------------------------------------------------------------
  def close
    ICH.call(@h_connect)
  end
  
end
[/pre]

使用时:
[pre lang="ruby"]ftp = FTP.new("username","password","server IP") #建立连接
ftp.mkdir("test") #创建文件夹
ftp.chdir("/test") #改变当前目录
ftp.put_file("s.jpg","s.jpg",2) #上传图片文件
ftp.close[/pre]

点评

另外事实上直接用TCP去处理FTP协议也还是简单的,FTP就是一堆文本协议  发表于 2016-7-12 05:22
编码需要转换一下格式……简单地说, 用W结尾的API如FtpCreateDirectoryW, 然后字符串要转换成utf16le,方法是"你好".unpack("U*").pack("S*")  发表于 2016-7-12 05:21

评分

参与人数 1赞 +1 收起 理由
zyf722 + 1 塞糖

查看全部评分

梦石
0
星屑
9292
在线时间
2504 小时
注册时间
2011-5-20
回帖
14521

开拓者

发表于 2016-7-12 11:00:34 | 显示全部楼层
只有中文不行吗?还是英文以外都不行···
[img]http://service.t.sina.com.cn/widget/qmd/5339802982/c02e16bd/7.png
回复

使用道具 举报

头像被屏蔽
梦石
0
星屑
172
在线时间
935 小时
注册时间
2010-11-13
回帖
897
发表于 2020-6-25 07:54:30 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

梦石
0
星屑
4216
在线时间
619 小时
注册时间
2013-4-30
回帖
111
发表于 2020-6-25 08:24:41 | 显示全部楼层
前排%%%%%
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部