cancel
Showing results for 
Search instead for 
Did you mean: 

Java Backed Web Scripts - File Upload

mocathe1st
Champ in-the-making
Champ in-the-making
Hi all,

I've been trying to create a Java Backed Web Script that will allow clients to upload files via POST. The main problem I'm getting is that no parameters seem to be present in the request object when I send a form as a multipart/form-data. I've written the following code to test if anything's getting through which shows the number of items in the uploaded request:
public void execute(WebScriptRequest req, WebScriptResponse res)
        throws IOException
    {
       try
       {
          HttpServletRequest httpReq = ((WebScriptServletRequest)req).getHttpServletRequest();
           FileItemFactory factory = new DiskFileItemFactory();
           ServletFileUpload upload = new ServletFileUpload(factory);
          
           List items = upload.parseRequest(httpReq);
           res.getWriter().write("no items: "+items.size());
          
           Iterator iter = items.iterator();
          
           while(iter.hasNext())
           {
              FileItem item = (FileItem)iter.next();
              if(item.isFormField())
              {
                 res.getWriter().write("\nIs Form Field");
                 res.getWriter().write("\nField Name: "+item.getFieldName());
              }
              else
              {
                 res.getWriter().write("\nIs Uploaded File");
                 res.getWriter().write("\nField Name: "+item.getFieldName());
              }
           }
          
       }
       catch(Exception e)
       {
          res.getWriter().write("Error in processing web service\n\n"+
                e.getMessage()+"\n\n"+
                e.getStackTrace()+"\n\n"+
                e.getCause()+"\n\n"+
                "error class= "+e.getClass());
       }
    }

I've written a test HTML page with a multipart/form-data form that allows uploading a file via <input type="file" name="testfile"/> and I also send a hidden input as such: <input type="hidden" name="test" value="test"/>. I don't seem to be able to get access to either of these. Removing the enctype in the form and with removing the file input, I can access the hidden input using req.getParameter("test") but am not able to access it as above.

Any help would be greatly appreciated, I've been pulling my hair out with this for a while now. File uploading seems to be an absolute nightmare with Java, I just wish that Alfresco was written in PHP…. :x  (unfortunately, due to the requirements of the project I'm currently working, a Java solution has to be used)

Thanks,
Malcolm Murray
5 REPLIES 5

jbarmash
Champ in-the-making
Champ in-the-making
i am not sure what's going on, but in your test page, are you setting enc-type?

From:
http://htmlhelp.com/reference/html40/forms/input.html

The following example allows the user to upload an HTML document for validation:

<FORM METHOD=post ACTION="/cgi-bin/validate.cgi" ENCTYPE="multipart/form-data">
<P>Select an HTML document to upload and validate. If your browser does not support form-based file upload, use one of our <A HREF="methods.html">alternate methods of validation</A>.</P>
<P><INPUT TYPE=file NAME="html_file" ACCEPT="text/html"></P>
<P><INPUT TYPE=submit VALUE="Validate it!"></P>
</FORM>

mocathe1st
Champ in-the-making
Champ in-the-making
Yes, I'm setting ENCTYPE for the file upload. I assume that this means that I need a different set of classes for picking up the file at the Alfresco end. With the standard Spring Framework there is a MultiPartHttpRequest class that I would normally use, but this doesn't pick up the parameters when I use it with Alfresco.

I still haven't found a solution for this, any help would be greatly appreciated.

Best Regards,
Malcolm Murray

mocathe1st
Champ in-the-making
Champ in-the-making
Anyone got any ideas on this?

I still haven't found a solution… I'm probably missing something really obvious, but I'm struggling to find any info on file uploading with the Java Backed Web Scripts. Is this even possible using Java Backed Web Scripts?

risenhoover
Champ in-the-making
Champ in-the-making
I don't know if this is related, but I'm having a similar problem getting formdata parameters to my Javascript web scripts.  I'm using Java and JUnit as a test harness and sending POST's with formdata to my web scripts, but by the time it gets into the web script, the formdata item is emtpy.  In order for me to access the data, I have to use the args array.

rogier_oudshoor
Champ in-the-making
Champ in-the-making
The issue you're having is a generic Java Servlet issue in fact; that multi-part HTML forms do not end up visible in your servlet. Basically, you can do two things:
1. Use javascript (the formdata does support multi-part forms)
2. Use a servlet filter to pre-process the multi-part data for you

Cheers,

Rogier