Tuesday, February 5, 2008

Uploading File From Flex to Tomcat

To upload file from Flex to Java utilize the fileUpload utility provided by Apache. Go to http://commons.apache.org/fileupload/ to download the lib files and see documentation. This utility in turn depends on commons-io utitlity that you can get at http://commons.apache.org/io/ . Copy the jar files from these two utilities to WEB-INF/lib of your application.

In Flex use FileReference object to get the file to upload:

private var fileRef:FileReference;

private function fileBrowse():void{
   this.fileRef = new FileReference();
   fileRef.addEventListener(Event.SELECT,selectHandler);
   fileRef.browse();
}


private function selectHandler(event:Event):void{
   var request:URLRequest = new URLRequest("http://localhost:8080/FlexJava1/fileLoader.jsp");
   /*Map this to the servlet or jsp you wish to use to handle your file writing request*/
   fileRef.upload(request);
}




The jsp will be like this (O yes this should be done in a servlet but lazy me doing it in a jsp. But you be good and make a nice servlet :p):


<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.InputStream"%>

<%
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List /* FileItem */ items = upload.parseRequest(request);


// Process the uploaded items
Iterator iter = items.iterator();

while (iter.hasNext()) {
   FileItem item = (FileItem) iter.next();
   //handling a normal form-field
   if (item.isFormField()) {
      System.out.println("Got a form field");
      String name = item.getFieldName();
      String value = item.getString();
      System.out.println("Name:"+name+",Value:"+value);
   } else {//handling file loads
      System.out.println("Not form field");
      String fieldName = item.getFieldName();
      String fileName = item.getName();
      String contentType = item.getContentType();
      boolean isInMemory = item.isInMemory();
      long sizeInBytes = item.getSize();
      System.out.println("Field Name:"+fieldName+",File Name:"+fileName);
      System.out.println("Content Type:"+contentType+",Is In Memory:"+isInMemory+",Size:"+sizeInBytes);

      byte[] data = item.get();
      FileOutputStream fileOutSt = new FileOutputStream("outagain.jpg");
      fileOutSt.write(data);
      fileOutSt.close();
   }
}
%>


You will find your file written under the Tomcat root installation folder

11 comments:

Anonymous said...

This was exactly what I was looking for. Thank you very much!

Anonymous said...

Thanks alot sir !! was struggling with this. Seriously great work

Saveen said...

I am glad that it was of some help :). cheers!

Anonymous said...

Excellent!!!
Thanks a million for your contribution. You make my day.
KC

Anonymous said...

hooo you realy make my day ... thaxxxxx

Tom said...

Perfect. Thanks.

For people wanting to write to a specific sub-directory, try...

fileName = getServletContext().getRealPath( ###SUB-DIR HERE### + fileName);

before


FileOutputStream fileOutSt = new FileOutputStream(fileName);

KS said...

Hi Saveen,

Yes, it works wonders.

Had been banging my head over upload and file reference with a stupid servlet that was not working.

Thanks a lot !

Saveen said...

I am glad it was of some use to you :). Cheers!

Anonymous said...

Hi Saveen,
Thanks for the example. its great!!!

This is working fine when called in Application tag of flex but when i m trying to do the same in Module component and calling this module as child of other component it is even not calling my jsp. I checked it in firebug..
Please help me in this issue

Thanks!!!

Anonymous said...

Wow ! I tried with FileReference upload with flash 10 during 2 days with no results but finally works with you're solution. Thanks a lot

Anonymous said...

i tried this code for flex mobile application n it worked when i uploaded it from simulator but when i tested it from device it is not getting uploaded...please help me out... :(