Bytes, strings and encodings...

You're close, try this:

content = Base64.encode64(File.read(filePath))

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

Rob Biedenharn wrote:

You're close, try this:

content = Base64.encode64(File.read(filePath))

tried, but the results from Java and this code do not match.. Any ideas
why?

If you're on Windoze, perhaps:
  f = File.open(filePath, 'rb')
  encoded = f.read
  f.close
  content = Base64.encode64(encoded)

Or take the Base64 out of the picture and make sure that you get the same result on a plain file. If you have a difference in the length of the output by 4/3 of the number of lines in the original, it's very likely a line ending difference.

Other than that, no one is going to be able to help without more information. Perhaps create a small file and post the result if you can't figure it out from there.

$ irb
>> require 'base64'
=> true
>> filename = "sample.txt"
=> "sample.txt"
>> File.open(filename, "wb") { |f| f.write "hello, world\n" }
=> 13
>> puts Base64.encode64(File.read(filename))
aGVsbG8sIHdvcmxkCg==
=> nil
>> puts Base64.decode64("aGVsbG8sIHdvcmxkCg==")
hello, world
=> nil
>> p Base64.decode64("aGVsbG8sIHdvcmxkCg==")
"hello, world\n"
=> nil

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com