commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Scott Dudley <sc...@telesoft.com>
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"
    value="<%=request.getParameter("directory")%>">
    --%>
    <input type="file" name="fileName">
    <input type="Submit" name="Submit" value="Upload">

    </form>

 From FileUploadServlet.java:

        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 " +
    directory.getName());

            DiskFileUpload fu = new DiskFileUpload();

            // maximum size before a FileUploadException will be thrown
            fu.setSizeMax(1000000);

            // maximum size that will be stored in memory
            fu.setSizeThreshold(4096);

            // the location for saving data that is larger than
    getSizeThreshold()
            fu.setRepositoryPath(directory.getAbsolutePath());

            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) {
               
                e.printStackTrace();

                throw new ServletException(e.getMessage());
            }
        }

-- 

Regards,

Scott Dudley



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message