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-file upload
Date Wed, 05 Mar 2003 19:50:23 GMT

"Ip, Ting Shing" <TingShing.Ip@maniabarco.com> wrote in message
news:E78C42D5FF60D611AF870002A5ECEFEA5B87EB@mbgemex01.maniabarco.com...
> Hello
>
> I have the follwing code with upload one file  to "d:\\test\\testfile".
> After the upload, i have opened  the file "d:\\test\\testfile". it was
> totally not the original file. It contains a string "upload". WHat is
wrong
> with mijn code.

Firs, you say above that you are uploading one file, but the comments in
your code say that you are assuming two files. Second, you are not checking
the FileItem instances to see if they are actually files (as opposed to
other HTML form fields). Since you didn't post the page you are using to
submit the request, I can't tell whether that is the cause of the problem
you are seeing, or if it's something else.

In any case, I would strongly suggest that you refactor your code to cleanly
separate the request processing from the response generation. The way it is
now, it's not that easy to figure out just what you are trying to do.

--
Martin Cooper


>
>
>   public void doPost(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
>     response.setContentType(CONTENT_TYPE);
>     PrintWriter out = response.getWriter();
>
>       try {
>       ControllerResponse res = (ControllerResponse) request.getAttribute(
>                                   Controller.RESPONSE_KEY);
>
>       FileUpload fu = new FileUpload();
>       // maximum size before a FileUploadException will be thrown
>       fu.setSizeMax(999999999);
>       // maximum size that will be stored in memory
>       fu.setSizeThreshold(4096);
>       // the location for saving data that is larger than
getSizeThreshold()
>
>
fu.setRepositoryPath(Setup.getValueRequired(super.getDBName(request),
>                                                     "TempDir"));
>
>       List fileItems = fu.parseRequest(request);
>       // assume we know there are two files. The first file is a small
>       // text file, the second is unknown and is written to a file on
>       // the server
>       Iterator i = fileItems.iterator();
>
>       FileItem fi;
>       String fileName;
>       File file;
>       // write the file
>       out.println("<html>");
>       out.println("<head><title>FileUpload</title></head>");
>       out.println("<body>");
>       out.println("<p>The servlet has received a POST. This is the
> reply.</p>");
>       while(i.hasNext()){
>         fi = (FileItem)i.next();
>         fileName = fi.getName();
>         //file = new File(fileName);
>         fi.write("d:\\test\\testfile);
>         out.println("<p>Uploading file:  " + fileName +"<p>");
>       }
>       out.println("<p>The servlet has received a POST. This is the
> reply.</p>");
>       out.println("</body></html>");
>       out.flush();
>     } catch(DBException dbe) {
>       throw new ServletException("Tempory directory has not been defined
in
> the SETUP table", dbe);
>     } catch (Exception fue) {
>       throw new ServletException( fue);
>     }
>
>   }
> - - - - - - - - DISCLAIMER - - - - - - - -
> Unless indicated otherwise, the information contained in this message is
> privileged and confidential, and is intended only for the use of the
> addressee(s) named above and others who have been specifically authorized
to
> receive it. If you are not the intended recipient, you are hereby notified
> that any dissemination, distribution or copying of this message and/or
> attachments is strictly prohibited. The company accepts no liability for
any
> damage caused by any virus transmitted by this email. Furthermore, the
> company does not warrant a proper and complete transmission of this
> information, nor does it accept liability for any delays. If you have
> received this message in error, please contact the sender and delete the
> message. Thank you.
>




Mime
View raw message