Use apache commons FileUpload API!!

It works !!

On Wed, Mar 4, 2009 at 9:53 AM, akshay <[email protected]> wrote:

>
> Hi all,
>      Can anyone tell me whats the problem ??
>
> On Mar 3, 7:34 pm, akshay <[email protected]> wrote:
> > Hi all,
> >         Is there a problem of  content-type?? Pl help me..:(
> >
> > Thanks in advance
> > Akshay
> >
> > On Mar 3, 6:17 pm, akshay <[email protected]> wrote:
> >
> > > Hi all,
> > >          I am using FileReference in my application.. In bachend i am
> > > using servlet.. now the problem is that I am able to call servlet
> > > successfully.. but i am getting error for following line
> >
> > > FileItem items = upload.parseRequest( req );
> > > where upload is object of ServletFileUpload..
> >
> > > Error is :
> > > IOErrorEvent type="ioError" bubbles=false cancelable=false
> > > eventPhase=2 text="Error #2038: File I/O Error. URL:
> http://localhost:8400/FileUpload/uploadservlet";
> >
> > > Pl tell me where am i wrong?? Pl help me ..
> >
> > > My servlet code is:
> >
> > > // Core classes
> > > import java.io.File;
> > > import java.io.IOException;
> > > import java.util.Iterator;
> > > import java.util.List;
> >
> > > // Servlet classes
> > > import javax.servlet.ServletOutputStream;
> > > import javax.servlet.http.HttpServlet;
> > > import javax.servlet.http.HttpServletRequest;
> > > import javax.servlet.http.HttpServletResponse;
> >
> > > // Commons classes
> > > import org.apache.commons.fileupload.FileItem;
> > > import org.apache.commons.fileupload.FileItemFactory;
> > > import org.apache.commons.fileupload.FileUploadException;
> > > import org.apache.commons.fileupload.disk.DiskFileItemFactory;
> > > import org.apache.commons.fileupload.servlet.ServletFileUpload;
> >
> > > // Sample file upload servlet
> > > public class UploadFileServlet extends HttpServlet
> > > {
> > >         // Called when a file has been submitted
> > >         // Called because file uploads are HTTP POST operations
> > >         public void doPost( HttpServletRequest req, HttpServletResponse
> res )
> > >         {
> > >                 // Setup the various objects used during this upload
> operation
> > >                 // Commons file upload classes are specifically
> instantiated
> > >                 File                            disk = null;
> > >                 FileItem                        item = null;
> > >                 FileItemFactory         factory = new
> DiskFileItemFactory();
> > >                 Iterator                        iter = null;
> > >                 List                            items = null;
> > >                 ServletFileUpload       upload = new ServletFileUpload(
> factory );
> > >                 ServletOutputStream     out = null;
> >
> > >
> System.out.println("=======================================Got
> > > request========================================");
> > >                 /*File f = new File("signal.sig");
> > >                 try{
> > >                         f.createNewFile();
> > >                 }catch(java.io.IOException exception){}*/
> > >                 try
> > >                 {
> > >                         // Parse the incoming HTTP request
> > >                         // Commons takes over incoming request at this
> point
> > >                         // Get an iterator for all the data that was
> sent
> > >                         // TODO: Leverage generics
> > >
> System.out.println("=======================================Before
> > > parsing ========================================");
> > >                         items = upload.parseRequest( req );
> > >
> System.out.println("=======================================After
> > > parsing ========================================");
> > >                         iter = items.iterator();
> >
> > >                         // Set a response content type
> > >                         res.setContentType( "text/xml" );
> >
> > >                         // Setup the output stream for the return XML
> data
> > >                         out = res.getOutputStream();
> > >                         out.println( "<response>" );
> >
> > >                         // Iterate through the incoming request data
> > >                         while( iter.hasNext() )
> > >                         {
> > >                                 // Get the current item in the
> iteration
> > >                                 item = ( FileItem )iter.next();
> >
> > >                                 // If the current item is an HTML form
> field
> > >                                 if( item.isFormField() )
> > >                                 {
> > >                                         // Return an XML node with the
> field name and value
> > >                                         out.println( "<field name=\"" +
> item.getFieldName() + "\" value=
> > > \"" + item.getString() + "\" />"  );
> >
> > >                                 // If the current item is file data
> > >                                 } else {
> > >                                         // Specify where on disk to
> write the file
> > >                                         // Using a servlet init param
> to specify location on disk
> > >                                         // Write the file data to disk
> > >                                         // TODO: Place restrictions on
> upload data
> > >                                         disk = new File(
> getInitParameter( "uploadPath" ) + item.getName
> > > () );
> > >                                 //      disk = new
> file(getServletContext().getRealPath(getInitParameter
> > > (”uploadPath”)) + “\\” + item.getName() );
> > >                                         item.write( disk );
> >
> > >                                         // Return an XML node with the
> file name and size (in bytes)
> > >                                         out.println( "<file name=\"" +
> item.getName() + "\" size=\"" +
> > > item.getSize() + "\" />"  );
> > >                                 }
> > >                         }
> > >                         // Close off the response XML data and stream
> > >                         out.println( "</response>" );
> > >                         out.close();
> >
> > >                 // Rudimentary handling of any exceptions
> > >                 // TODO: Something useful if an error occurs
> > >                 } catch( FileUploadException fue ) {
> > >                         fue.printStackTrace();
> > >                 } catch( IOException ioe ) {
> > >                         ioe.printStackTrace();
> > >                 } catch( Exception e ) {
> > >                         e.printStackTrace();
> > >                 } finally{
> > >                 }
> >
> > >         }
> >
> > >         public void doGet( HttpServletRequest req, HttpServletResponse
> res )
> > >         {
> > >                 doPost(req,res);
> > >         }
> >
> > > }
> >
> > > My ActionScript Code:
> >
> > >             uploadURL.url = "http://localhost:8400/FileUpload/
> > > uploadservlet";
> > >             file = FileReference(event.target);
> > >             file.upload(uploadURL,"Filedata",false);
> >
> > > It goes upto the "before parsing" stmt after that its giving me the
> > > error ..
> > > Pl help me ..
> >
> > > Thanks in advance
> > > Regards,
> > > Akshay
> >
>


-- 
With Best wishes,
Amrish.S.Jaiswal.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Flex 
India Community" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/flex_india?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to