Page 1 of 1

using writeFile to write binary data

Posted: 06 Oct 2010, 18:21
by mistermind
Hi there,
I got a tricky one here:
I'm trying to create a jpg file in the server by using the _server.writeFile.
I'm converting the ByteArray to Base64 on the client, sending it via JSON to the server, then converting it back to binary by decoding it.

Code: Select all

_server.writeFile("c:\\test64.jpg", decode64(params.im));
The Base64 string is working just fine and I tested several times, but the problem is when I write the decoded string into a file. The file always get corrupted.
Here is a website you can test it out:

http://decodebase64.com/ (use the example provided in the first comment)

If you try to decode that exact string into bytes and write it to a jpg file, it comes out corrupted.

Posted: 08 Oct 2010, 14:00
by Lapo
In order to write binary files you should use the Java classes.
One quick way to do it would be to download the Apache Commons IO library which offers very high level methods to read/write files. It is very powerful and allows file access in one line of code.

Steps to follow:
-- download the library ( http://commons.apache.org/io/ )
-- add the jar to the lib/ folder in SFS and add the reference to the classpath
-- import the FileUtils class in your code and enjoy :)

Posted: 08 Oct 2010, 15:57
by mistermind
ow yeah :D
thats why I made this tutorial:
http://forums.smartfoxserver.com/viewtopic.php?t=8656

It pretty much does what you mentioned. I even posted a java class ready to be used.

YOU enjoy! :lol: