In a Java/Flex project i have been working on we need to upload and download images, simply task no? Yes... is simple we use the FileReference class and point this to a servlet then simply use the upload method.
Really easy when you run in your local machine but what happens when we run this in a remote machine... let me think... ah! yes the lovely Flash Player Sandbox Security Exception... ok! we can handle it, just use crossdomain.xml file and run your application again and... Flash Player Sandbox Security Exception again (dear this is becoming my best friend), then i read, to upload a file Flash player uses the socket api and now we need a socket policy file... so i give up and try to do it in another way.
Today i show you how you can do a kind of "upload" and "show" a remote file using Remote Objects and the lovely BlazeDS. So let's start
We use a Java class (not servlet) to manage the upload and the get of a file.
In the Java side we have two methods uploadFile and getFile, the first one uses the name, the directory and the content as an byte array to write a file in the disk using the FileOutputStream class. For the second method we need the full file name (directory + file name) and use the FileInputStream to read it and put the data in a byte array and the send it to Flex.
Don't forget to change your remoting-config.xml :-)
And in the flex side we still use the FileReference but this time only for load the file, and then we use remote objects to send the chunk of data, the file name and the directory to the upload method, for getting the file the java side just send the chunk of data and we manage it with a ByteArray and show it.
That's all now you can upload a file using a remote object and show this file with this two methods, and remember to secure your endpoints and externalize your server configuration. Any question or mistakes just let me know. Thanks for reading.