Project1

标题: 在RM中访问FTP(上传/下载文件) [打印本页]

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

脚本
RUBY 代码复制
  1. module Net
  2.   #需要用到的部分常数
  3.   AGENT = "RGSS"  #调用WinINet函数的应用程序
  4.   INTERNET_OPEN_TYPE_PRECONFIG = 0  #使用IE中的连接设置
  5.   INTERNET_SERVICE_FTP = 1  #指定服务类型为FTP
  6.   INTERNET_DEFAULT_FTP_PORT = 21  #FTP服务器的默认端口
  7.   #需要用到的API函数
  8.   IO = Win32API.new('wininet','InternetOpen','plppl','l')
  9.   IC = Win32API.new('wininet','InternetConnect','lplpplll','l')
  10.   ICH = Win32API.new('wininet','InternetCloseHandle','l','i')
  11.   FCD = Win32API.new('wininet','FtpCreateDirectory','lp','i')
  12.   FDF = Win32API.new('wininet','FtpDeleteFile','lp','i')
  13.   FGCD = Win32API.new('wininet','FtpGetCurrentDirectory','lpp','i')
  14.   FGF = Win32API.new('wininet','FtpGetFile','lppilll','i')
  15.   FPF = Win32API.new('wininet','FtpPutFile','lppll','i')
  16.   FRD = Win32API.new('wininet','FtpRemoveDirectory','lp','i')
  17.   FRF = Win32API.new('wininet','FtpRenameFile','lpp','i')
  18.   FSCD = Win32API.new('wininet','FtpSetCurrentDirectory','lp','i')
  19.  
  20. end
  21.  
  22. class FTP
  23.   include Net
  24.   #--------------------------------------------------------------------------
  25.   # ● 初始化
  26.   #--------------------------------------------------------------------------
  27.   def initialize(user,passwd,server,port = INTERNET_DEFAULT_FTP_PORT)
  28.     #初始化
  29.     @h_internet = IO.call(AGENT,INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0)
  30.     #建立FTP Session
  31.     @h_connect = IC.call(@h_internet,server,port,user,passwd,INTERNET_SERVICE_FTP,0,0)
  32.   end
  33.   #--------------------------------------------------------------------------
  34.   # ● FtpCreateDirectory 新建文件夹
  35.   #     dir : 新文件夹名
  36.   #--------------------------------------------------------------------------
  37.   def mkdir(dir)
  38.     FCD.call(@h_connect,dir)
  39.   end
  40.   #--------------------------------------------------------------------------
  41.   # ● FtpDeleteFile 删除服务器上的文件
  42.   #     file : 目标文件名
  43.   #--------------------------------------------------------------------------
  44.   def delete(file)
  45.     FDF.call(@h_connect,file)
  46.   end
  47.   #--------------------------------------------------------------------------
  48.   # ● FtpGetCurrentDirectory 获取当前目录
  49.   #--------------------------------------------------------------------------
  50.   def pwd
  51.     path = "\0" * 250
  52.     FGCD.call(@h_connect,path,250)
  53.     path.delete("\0")
  54.   end
  55.   #--------------------------------------------------------------------------
  56.   # ● FtpGetFile 文件下载
  57.   #     remote_file : 远程文件名
  58.   #     new_file : 下载到本地后的文件名
  59.   #     fail_if_exists : 是否覆盖同名文件(为0时覆盖,为1时下载失败),默认为0
  60.   #     attr : 下载后文件的属性(默认为0x80,即FILE_ATTRIBUTE_NORMAL)
  61.   #     type : 指定传输方式(1 = ASCII, 2 = BINARY),默认为2
  62.   #--------------------------------------------------------------------------
  63.   def get_file(remote_file,new_file,fail_if_exists = 0,attr = 0x80,type = 2)
  64.     FGF.call(@h_connect,remote_file,new_file,fail_if_exists,attr,type,0)
  65.   end
  66.   #--------------------------------------------------------------------------
  67.   # ● FtpPutFile 上传文件至服务器
  68.   #     local_file : 本地待上传文件名
  69.   #     new_remote_file : 上传到服务器后的文件名
  70.   #     type : 指定传输方式(1 = ASCII, 2 = BINARY),默认为2
  71.   #--------------------------------------------------------------------------
  72.   def put_file(local_file,new_remote_file,type = 2)
  73.     FPF.call(@h_connect,local_file,new_remote_file,type,0)
  74.   end
  75.   #--------------------------------------------------------------------------
  76.   # ● FtpRemoveDirectory 删除服务器上的文件夹
  77.   #     dir : 目标文件夹名
  78.   #--------------------------------------------------------------------------
  79.   def rmdir(dir)
  80.     FRD.call(@h_connect,dir)
  81.   end
  82.   #--------------------------------------------------------------------------
  83.   # ● FtpRenameFile 重命名服务器上的文件
  84.   #     existing : 目标文件名
  85.   #     new_name : 新文件名
  86.   #--------------------------------------------------------------------------
  87.   def rename(existing,new_name)
  88.     FRF.call(@h_connect,existing,new_name)
  89.   end
  90.   #--------------------------------------------------------------------------
  91.   # ● FtpSetCurrentDirectory 更改当前目录
  92.   #     dir : 新路径
  93.   #--------------------------------------------------------------------------
  94.   def chdir(dir)
  95.     FSCD.call(@h_connect,dir)
  96.   end
  97.  
  98.   #--------------------------------------------------------------------------
  99.   # ● InternetCloseHandle 关闭连接
  100.   #--------------------------------------------------------------------------
  101.   def close
  102.     ICH.call(@h_connect)
  103.   end
  104.  
  105. end


使用时:
RUBY 代码复制
  1. ftp = FTP.new("username","password","server IP") #建立连接
  2. ftp.mkdir("test") #创建文件夹
  3. ftp.chdir("/test") #改变当前目录
  4. ftp.put_file("s.jpg","s.jpg",2) #上传图片文件
  5. ftp.close

作者: chd114    时间: 2016-7-12 11:00
只有中文不行吗?还是英文以外都不行···
作者: 574656549    时间: 2020-6-25 07:54
提示: 作者被禁止或删除 内容自动屏蔽
作者: zyf722    时间: 2020-6-25 08:24
前排%%%%%




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1