I am trying to download certain files from a remote server using Ruby's Net::FTP class here is my code
require 'net/ftp'
ftp = Net::FTP.new
ftp.connect('mydomain', '21')
ftp.login('username', 'password')
ftp.passive = true
ftp.chdir("testing")
ftp.getbinaryfile('個人情報.csv', 'test1.csv')
puts "#{Time.now} > Downloaded 個人情報.csv."
ftp.getbinaryfile('住所.csv', 'test2.csv')
puts "#{Time.now} > Downloaded 住所.csv"
ftp.close
I am getting the following errors
$ ruby ftp_download.rb
/usr/local/lib/ruby/2.3.0/net/ftp.rb:340:in `getresp': 451 No mapping for the Unicode character exists in the target multi-byte code page. (Net::FTPTempError)
from /usr/local/lib/ruby/2.3.0/net/ftp.rb:367:in `block in sendcmd'
from /usr/local/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
from /usr/local/lib/ruby/2.3.0/net/ftp.rb:365:in `sendcmd'
from /usr/local/lib/ruby/2.3.0/net/ftp.rb:424:in `transfercmd'
from /usr/local/lib/ruby/2.3.0/net/ftp.rb:501:in `block (2 levels) in retrbinary'
from /usr/local/lib/ruby/2.3.0/net/ftp.rb:214:in `with_binary'
from /usr/local/lib/ruby/2.3.0/net/ftp.rb:499:in `block in retrbinary'
from /usr/local/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
from /usr/local/lib/ruby/2.3.0/net/ftp.rb:498:in `retrbinary'
from /usr/local/lib/ruby/2.3.0/net/ftp.rb:632:in `getbinaryfile'
from ftp_download.rb:26:in `<main>'
The tagrget files are SJIS encoded.
What am i doing wrong ? any help much appreciated! Thank you
The error message comes from the FTP server. The problem is that the FTP server cannot interpret these unicode filenames. Please check if UTF8 feature is enabled for the connection.
From an existing answer on stackoverflow:
It is not enough for you just to encode your string as UTF8 and send it as filename to FTP server. In the past all FTP servers understood ASCII only and nowadays to maintain backward compatibility - even if they are Unicode aware - when they start they treat all filenemes as ASCII too.
To make it all work you (your program) must first check what your server is capable of. Servers send their features after client connects - in your case you must check for FEAT UTF8. If your server sends that - it means it understands UTF8. Nevertheless - even if it understands it - you must tell it explicitly that from now on you will send your filenames UTF8 encoded and now it is the stuff that your program lacks (as your server supports utf8 as you've stated).
Your client must send to FTP server the following OPTS UTF8 ON. After sending that you may use UTF8 or speak UTF8-ish (so to speak) to your sever.
Read here for details Internationalization of the File Transfer Protocol
Sources: