OOP-ResearchMake It Simpler by Object Oriented Programming

Where to save the uploaded files by OOP MimeParser: Java API for file upload JSP/Servlet by multipart/form-data

Java API for file upload JSP/Servlet by multipart/form-data. It gives your JSP/Servlet the full control over where to save the uploaded files. The unique sub-directory can be allocated per user.
This page describes where to save the uploaded files by OOP MimeParser.

Related Pages:


Where to save the files

When you call any of parseAndSave(...) methods, the uploaded files will be saved on the server side, according to the rule described in thie page. This rule consists of:

  • The destination directory
  • The file name
On the contrary, if you call any of parseOnly(...), it is your responsibility to save the uploaded files, because these methods do not save the uploaded files by themselves.

Destination directory

When you call any of:

  • parseAndSave(HttpServletRequest req, String dir)
  • parseAndSave(HttpServletRequest req, String dir, int max)
  • parseAndSave(HttpServletRequest req, String dir, Set mime)
  • parseAndSave(HttpServletRequest req, String dir, int max, Set mime)
  • parseAndSave(HttpServletRequest req, String ch, String dir)
  • parseAndSave(HttpServletRequest req, String ch, String dir, int max)
  • parseAndSave(HttpServletRequest req, String ch, String dir, Set mime)
  • parseAndSave(HttpServletRequest req, String ch, String dir, int max, Set mime)
the uploaded files will be saved under:
  • (root directory)/dir
By the second or third parameter of the above 8 methods, you can specify the sub-directory where the uploaded files will be saved. If the null is specified as these parameters, the uploaded files will be saved just under root directory. In any case, if the intended directory does not exist, MimeParser will create it on demand. Note that root directory must be specified in:
  • (docBase)/WEB-INF/classes/MimeParser.properties
For details about this property file, please consult the list of the properties.

For example, if you are on the UNIX, root directory will be something like:
  • /home/foo/mime
When you specify "bar" as the second or third parameter of the above methods, the uploaded files will be saved under:
  • /home/foo/mime/bar
In case of Windows OS, root directory will be something like:
  • C:\\mywebapps\mime
and the destination directory will be:
  • C:\\mywebapps\mime\bar
In the typical JSP/Servlet programming, the user is identified by some Java object in HttpSession. For example, the code fragment of your JSP will look like this:


  // Get the list of the acceptable mime-types...
  Set accept_mime=AcceptMime.getAcceptMime();

  // Get the user name...
  HttpSession session=req.getSession(true);
  String user=(String)(session.getAttribute("user"));

  // Save the uploaded files under the sub-directory
  // which is allocated per user...
  mime.parseAndSave(request,user,accept_mime);

File name

When the user sends the file upload request from the web browser, the web browser detects the file name on the client side and sends it along with the request.

On the server side, MimeParser picks the file name sent by the web browser and saves the uploaded file by this file name. As a result, the uploaded file will be saved by the file name same as the client side.

But, you may want to save the uploaded file by the file name different from the file name on the cleint side. And MimeParser gives you a chance to specify the file name, independently from the file name on the client side.
If the HIDDEN INPUT or TEXT INPUT whose parameter name is same as the FILE INPUT is included in the FORM, MimeParser is wise enough to detect it. And the value of such a HIDDEN (or TEXT) INPUT will be used as the file name on the server side. For example:

FORM for file upload with file name by JSP
Note that both INPUT has the same name. In this example, the uploaded file is always saved as userphoto.gif on the local file system, regardless of the file name of the client side. Only the first HIDDEN INPUT before the FILE INPUT can be picked up as the file name by MimeParser.

Caution!
All the APIs for Servlet/JSP introduced by this web site are now included in Bento framework:
  • Simpler than JSTL or Apache Struts
  • MVC framework by HTML
  • Input validation from CGI FORM
  • Easy user authentication
  • Easy localization (L10n)
To download the APIs and source code examples, please visit the web site of Bento framework.


JBuilder 2007


General Information

For Java Development

Java and all Java-based trademarks and logos are trademarks or registered of Sun Microsystems, Inc. in the United States and other countries.


ALL CONTENTS COPYRIGHT 1997-2007, OOP-Research Corporation. All rights reserved.