commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject [FileUpload] Form parameters after a file input parameter not beingprocessed
Date Wed, 22 Aug 2007 12:44:17 GMT
I have a simple form that contains three input fields, one hidden, one 
checkbox, and one of type file.  If I place the hidden and checkbox input 
fields in front of the file input field then the servlet code picks up the 
regular form fields.  If one or both of these regular form fields are 
placed after the file input field then FileUpload doesn't see them.

Can regular form fields be placed after a file input field?  How can I get 
this working so I can have other form fields after the file input field?

Versions being used:
commons fileupload - 1.2
commons io - 1.3.2
jdk 6

Sample HTML form (the hidden & checkbox wont get picked up unless placed 
before the file input parm):

        <form name="frm_upload" method="post" 
action="WFImageUploadServlet" enctype="multipart/form-data">

                        <td>Image File Name:</td>
                                <input type="file" id="imageFileName" 
name="imageFileName" size="50" value="">

        <input type="hidden" id="imageId" name="imageId" value="" />

        <input type="checkbox" id="imageOverwrite" name="imageOverwrite" 
value="overwrite" />Overwrite Image on Server

        <input type="submit" value="Upload">


Servlet code:

        public void doPost(HttpServletRequest request, HttpServletResponse 
        throws IOException, ServletException
                // Check that we have a file upload request
                boolean isMultipart = ServletFileUpload.isMultipartContent
                if (!isMultipart)
                        throw new ServletException("HTML Form is not a 
multipart form");

                // Create a factory for disk-based file items
                DiskFileItemFactory factory = new DiskFileItemFactory();

                // Set factory constraints
                // Sets the size threshold beyond which files are written 
directly to disk.
                // Sets the directory used to temporarily store files that 
are larger than the configured size threshold
                factory.setRepository(new File("c:\\temp")); 

                // Create a new file upload handler
                ServletFileUpload upload = new ServletFileUpload(factory);

                // Set overall request size constraint
                // Sets the maximum allowed size of a complete request. -1 
is no limit
                // Sets the maximum allowed size of a single uploaded file

                // Parse the request
                List /* FileItem */ items = null;
                try {
                        items = upload.parseRequest(request); 
                } catch(FileUploadException fue) {
                        throw new ServletException(fue.getMessage());

                // Process the uploaded items
                Iterator iter = items.iterator();
                while (iter.hasNext()) {
                        FileItem item = (FileItem);

                        if (item.isFormField()) {  // this is a regular 
form field
                                String name = item.getFieldName();
                                String value = item.getString();

                                if (logger.isDebugEnabled()) {
                                        logger.debug("Parameter name: " + 
                                        logger.debug("Parameter value: " + 

                                if (name.equalsIgnoreCase("imageId"))
                                        imageId = value;
                                if (name.equalsIgnoreCase("imageOverwrite"
                                        imageOverwrite = true;
                        } else { // this is the file attachment
                                String fieldName = item.getFieldName();
                                String fileName = item.getName();
                                String contentType = 
                                boolean isInMemory = item.isInMemory();
                                long sizeInBytes = item.getSize();

                                if (logger.isDebugEnabled()) {
                                        logger.debug("Parameter: " + 
                                        logger.debug("File name: " + 
                                        logger.debug("Content type: " + 
                                        logger.debug("Is in memory: " + 
                                        logger.debug("Size in bytes: " + 

                                String imageDirectory = "c:\\temp\\";

                                File uploadedFile = new 
File(imageDirectory + fileName);

                                // save the image file attachment to the 
file system
                                try {
                                } catch (Exception e) {
                                        logger.error("Error saving image 
to file system", e);
                                        throw new ServletException("Could 
not save image to the file system");


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message