DownloadServlet v. 2.2


Simple component you may use for downloading files regardless of their location. How to use it:

1) download downloadPackage.jar and save it in WEB-INF/lib

2) describe Download servlet in web.xml file  


    <servlet>
     <servlet-name>DownloadServlet</servlet-name>
     <servlet-class>com.jsos.download.DownloadServlet</servlet-class>
    </servlet>

3) define a mapping:
 


    <servlet-mapping>
     <servlet-name>DownloadServlet</servlet-name>
     <url-pattern>/servlet/DownloadServlet</url-pattern>
    </servlet-mapping>

and now you can use it:
 


http://your_host/servlet/DownloadServlet?file_name

servlet will download this file from the server. By default servlet will search file in the root directory of your container. You may set an initial parameter described another directory. Parameter name is dir. For example:
 


    <servlet>
     <servlet-name>DownloadServlet</servlet-name>
     <servlet-class>com.jsos.download.DownloadServlet</servlet-class>
     <init-param>
      <param-name>dir</param-name>
      <param-value>full_path_to_your_directory</param-value>
     </init-param>
    </servlet>

and this directory (file store) could be located even the outside of your web application.

You can provide the following additional parameters for this servlet:

gzip - enable/disable data compression during the downloading. Possible values are true and false. Default value is false
mime - enable/disable mime type detection. Possible values are true and false. If this value is true than servlet will set mime type according to the file extension. Default value is false (mime type is application/octet-stream).
set-mime - lets you set a fixed mime type for all files
error - lets you describe some URL users requests will be redirected to in case of missed files. If this parameter starts with http:// than servlet redirects the request. Otherwise servlet assumes the local resource and request will be forwarded. By default is empty (the standard error message will be provided).
size - lets you describe a maximal size for the downloading (in bytes). By default the whole file will be downloaded.
disposition - enable/disable Content-disposition header. Possible values are true or false. By default is enabled (true). This header lets you use file name during the downloading. You may disable it for the mobile downloading, since some phones refuse to accept content when using it.
expires - describes a caching time (in seconds) for the images (client side cache).

For example:
 


    <servlet>
     <servlet-name>DownloadServlet</servlet-name>
     <servlet-class>com.jsos.download.DownloadServlet</servlet-class>
     <init-param>
      <param-name>gzip</param-name>
      <param-value>true</param-value>
     </init-param>
    </servlet>

For JSP see also Download taglib in Coldtags suite

For downloading:

servlet: downloadPackage.jar  
 

 © Coldbeans    Comments?
 

See also JSOS - the largest collection of servlets and filters.

Also in Coldtags: