Java servlet lets you upload file. You must just set this servlet as an 'action' parameter for your form. E.g. your form may looks so: It is a correspondent html-text:
<center>File upload test</center>
Also you can set a target for your form to some frame and provide a JavaScript callback for upload servlet.
Servlet will upload file and call your custom JavaScript function:
<table><tr> <iframe name="myframe" src="blank.html" style="border:0;height:0;width:0;padding:0;position: absolute;"></iframe> <form method="post" enctype="multipart/form-data" action="http://your_host/servlet/UploadServlet?your_config" target="myframe"> <td> <input type="file" size=20 name="fname"> </td> <td> <input type="Submit" value="Upload"> </td> </form> </tr></table> here myframe is a hidden frame. Servlet will upload file and call your own JavaScript function described in servlet's config file.
This callback is a function with 2 parameters: file name and error code (will be null in case of success). E.g.:
function myCallback(name,error) { if (error!=null) alert("Error code:"+error); else alert("Uploaded:"+name); } and name for this function (myCallback in this example) you have to describe in servlet's config file. Uploaded file can be emailed also to the specified address. How to use it: a) copy uploadPackage.jar into WEB-INF/lib directory. b) describe servlet in your web.xml file:
<servlet>
c) describe a mapping for this servlet in web.xml
<servlet-mapping> <servlet-name>UploadServlet</servlet-name> <url-pattern>/servlet/UploadServlet</url-pattern> </servlet-mapping> You must describe also a configuration file for this servlet. Configuration file describes various settings for your uploading. For example you may define where to save file, how to notify by email, what page should be next after uploading etc. You can pass configuration file as a parameter for servlet: http://your_host/servlet/UploadServlet?config_file
or define an initial parameter (parameter name is config):
<servlet>
and use servlet in this form http://your_host/servlet/UploadServlet Configuration file is a text file where the each line describes one parameter in the form of:
Empty lines and any line starts with # or // are ignored. You can use the following list of parameters in your configuration file: #
# name. Set the name for uploaded file. By default it will be original
file name
# Directory for saved files (by default it is . - in other words it
is a root directory
# log file
# maximal size for uploaded file (in bytes). By default size is unlimited
# extensions. Describes a commas separated list of allowed extensions for uploaded files.
# session: describes a name for session object.
# URL for redirection after uploading. See Notes below
# URL for redirection in case of some errors
# overwrite target file. 0 - do not overwrite and forward to error page, 1 - overwrite
# JavaScript callback. By default is empty (no callback)
# you can set also parameters for the email notification.
# smtp port (default value is 25)
# smtp host
# your domain
# source address for letter
# destination address for letter
# Subject line
# attach uploaded file to notification. 0 - do not attach, 1 - attach
Notes: 1. Servlet's configuration file can be saved anywhere on your server. E.g. if you are using UploadServlet?config line we assume this file is saved under the root directory of your servletrunner. But you can of course always use the full path for setting config file location: UploadServlet?/home/users/my_file (or UploadServlet?c:\users\my_file) 2. You can use query string for servlet in this form too:
3. If value for reply or error parameters starts with http than request will be redirected to that site. Otherwise servlet assumes a local resource and forwards request. 4. When you describe reply
parameter you may use meta-symbol $f also. It will be replaced
with encoded actual file name. E.g.:
For the redirection servlet saves a file name for uploaded file in the request scope also. The name for the appropriate attribute is uploadedFile. For the errors servlet passes error code in the parameter (or in the request scope attribute) uploadError a reason. Codes for reasons are: 0 - empty file, 1 - cannot create a new file, 2 - cannot overwrite a target file, 3 - not allowed file extension. 5. When you describe subject for notifications
you may use meta-symbol $f also. It will be replaced
with actual file name. E.g.:
6. Evaluation version restricts the size for uploaded files. For JSP pages see also Upload taglib For downloading: servlet: uploadPackage.jar Sample of configuration file: upconf
Unrestricted version: Buy this component © Coldbeans Software Comments? See also JSOS - the largest collection of servlets and filters.
|
Also in Coldtags:
|