commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Scott Dudley <>
Subject Commons FileUpload and DiskFileUpload.parseRequest()
Date Mon, 24 May 2004 20:56:35 GMT

I'm attempting to use org.apache.commons.fileupload in a servlet for the 
first time and have run into a snag.  See below for my doPost() method. 
 If my jsp form passes any parameter to the servlet other than the file, 
DiskFileUpload.parseRequest() returns an empty list.  If only the file 
is passed, it works splendidly.  What am I doing wrong?

 From jsp:

<form action="servlet/com.telesoft.web.FileUploadServlet" 
enctype="multipart/form-data" method="post">

    <input type="hidden" name="directory"
    <input type="file" name="fileName">
    <input type="Submit" name="Submit" value="Upload">



        public void doPost(HttpServletRequest req, HttpServletResponse
    res) throws ServletException, IOException {

            //String dirName = req.getParameter("directory");
            String dirName = "/tmp";
            if (dirName == null)
                throw new ServletException("Required parameter
    \"directory\" missing");
            File directory = new File(dirName);

            // guess we could just let these two issues fall to
    try/catch block...

            if (! directory.isDirectory())
                throw new ServletException(directory.getName() + " is
    not a directory");
            if (! directory.canWrite())
                throw new ServletException("Cannot write to " +

            DiskFileUpload fu = new DiskFileUpload();

            // maximum size before a FileUploadException will be thrown

            // maximum size that will be stored in memory

            // the location for saving data that is larger than

            try {
                List fileItems = fu.parseRequest(req);

                // if any paramters other than file meta data are passed
    to servlet, fileItems.size() == 0

                FileItem fi = (FileItem) fileItems.get(0);

                fi.write(new File(directory,  fi.getName()));

            } catch (Exception e) {

                throw new ServletException(e.getMessage());



Scott Dudley

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message