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>
<br>
<table><tr>
<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>
</tr></table>

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>
     <servlet-name>UploadServlet</servlet-name>
     <servlet-class>com.jsos.upload.UploadServlet</servlet-class>
    </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>
     <servlet-name>UploadServlet</servlet-name>
     <servlet-class>com.jsos.upload.UploadServlet</servlet-class>
     <init-param>
      <param-name>config</param-name>
      <param-value>path_to_your_configuration_file</param-value>
     </init-param>
    </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:
parameter=value

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
name=report.doc

# Directory for saved files (by default it is . - in other words it is a root directory
# for servletrunner)
dir=your_directory

# log file
log=path_to_your_file

# maximal size for uploaded file (in bytes). By default size is unlimited
max=10000

# extensions. Describes a commas separated list of allowed extensions for uploaded files.
# By default is empty (not restricted)
extensions=gif,png,jpg

# 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)
session=uploads

# URL for redirection after uploading. See Notes below
reply=http://your_host/your_page.htm

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

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

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

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

# smtp port (default value is 25)
port=25

# smtp host
host=your_smtp_host

# your domain
domain=your_domain

# source address for letter
from=you@your_domain

# destination address for letter
to=you@another_domain

# Subject line
subject=see attachment for uploaded file

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

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:
UploadServlet?config=your_config_file&name=file_name.
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.:
reply=http://www.dot.com?file=$f

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: