UploadServlet v. 2.8

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:

File upload test

It is a correspondent html-text:

<center>File upload test</center>
<form method="post" enctype="multipart/form-data" action="http://your_host/servlet/UploadServlet?your_config">
<td> <input type="file" size=20 name="fname"> </td>
<td> <input type="Submit" value="Upload"> </td> </form>

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:

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

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:


c) describe a mapping for this servlet in web.xml


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


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:

# Upload servlet 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
# for servletrunner)

# 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.
# By default is empty (not restricted)

# session: describes a name for session object.
# Servlet will save a name for uploaded file in current session so
# it will be available after redirection
# (see reply below).
# By default this parameter is empty (do not use session)

# URL for redirection after uploading. See Notes below

# URL for redirection in case of some errors
# during the uploading

# overwrite target file. 0 - do not overwrite and forward to error page, 1 - overwrite
# Default value is 1 (overwrite)

# JavaScript callback. By default is empty (no callback)

# you can set also parameters for the email notification.
# Uploaded file could be emailed as an attachment.

# smtp port (default value is 25)

# smtp host

# your domain

# source address for letter

# destination address for letter

# Subject line
subject=see attachment for uploaded file

# attach uploaded file to notification. 0 - do not attach, 1 - attach
# Default value is 1 (attach)


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:
This way you can provide a name for the uploaded file right in the query string. This option could be useful if you are generating upload forms (pages) dynamically.

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.:
subject=File $f has been uploaded

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: