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:
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:
When you specify "bar" as the second or third parameter of the above methods, the uploaded files will be saved under:
In case of Windows OS, root directory will be something like:
and the destination directory will be:
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:
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.
|
|