commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Cooper <mart...@apache.org>
Subject Re: Commons FileUpload and DiskFileUpload.parseRequest()
Date Mon, 31 May 2004 03:45:10 GMT
On Mon, 24 May 2004, Scott Dudley wrote:

>
> 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?

The problem isn't passing the additional parameter, it's attempting to
access it using request.getParameter(). As soon as you do that, the
container is going to consume the servlet input stream - meaning the
contents of the request - at which point it is no longer available for
FileUpload to parse.

Your 'directory' parameter will be available in the list of file items
returned by FileUpload, so you'll need to wait until then to use it.

--
Martin Cooper


>
>  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());
>             }
>         }
>
>

---------------------------------------------------------------------
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