viernes, 26 de agosto de 2011

Using BlazeDS Remote Object and Java to Upload and get Files

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.

FileUtils.java  


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.

6 comentarios:

  1. Hi Victor, i've used this code, and it works but... how could i use a progress bar for every file i want to upload??

    Hope you can help me, thanks in advance

    ResponderEliminar
  2. Hi you can't use progress bar with remote object, this is because you send a bunch of data at once, this is the behavior of Remote Object, you can use a busy cursor available as an option in Remote Object as indicator for your file uploading.

    ResponderEliminar
  3. Hi victor, thanks for the post, I have a question, in the section flex code there this code:
    var directory:String = "";

    here I have to write what dir????

    ResponderEliminar
  4. The dir variable is the server direcdtory path where you want to upload your file.

    ResponderEliminar
  5. Hi:
    I have to configure anything in web.xml, or remoting-config.xml? to CallResponder work?. please.

    ResponderEliminar
  6. If you're using BlazeDS you don't need to use any special configuration, just normal remoting.

    ResponderEliminar