Http-Poster plugin: server-side processing

Questions and answers on developing, deploying and using plugins and JavaBeans

Http-Poster plugin: server-side processing

Postby sdevlin » Thu Nov 04, 2004 10:27 pm

Can you give an example of how a servlet might process the poster plugin's doPost() method to write files to a directory on the server?

Thanks,
Sean
sdevlin
 
Posts: 125
Joined: Tue Apr 13, 2004 3:33 am

Postby Jan Blok » Fri Nov 05, 2004 11:14 am

pseudo/sample code
Code: Select all
import org.apache.commons.fileupload.DefaultFileItemFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;


   
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
    {

      String ct = request.getContentType();
      if (ct != null && ct.startsWith("multipart/form-data;")) //$NON-NLS-1$
      {
         File file = null;
         String fileName = null;
         
         FileUpload fileUpload = new FileUpload(new DefaultFileItemFactory());
         try
         {
            List list = fileUpload.parseRequest(request);
            Iterator iterator = list.iterator();
            while (iterator.hasNext())
            {
               FileItem fileItem = (FileItem) iterator.next();
               if (fileItem.isFormField())
               {
                  if ("file".equals(fileItem.getFieldName())) //$NON-NLS-1$
                  {
                     file = File.createTempFile("import", ".servoy"); //$NON-NLS-1$ //$NON-NLS-2$
                     fileItem.write(file);
                     fileName = fileItem.getName();
                  }
               }
            }
         }
         catch(Exception ex)
         {
         }

         if (file != null)
         {
            //process file
         }
      }
   }
Jan Blok
Servoy
Jan Blok
 
Posts: 2684
Joined: Mon Jun 23, 2003 11:15 am
Location: Amsterdam


Return to Plugins and Beans

Who is online

Users browsing this forum: No registered users and 40 guests