public abstract class FileServlet extends HttpServlet
The well known "BalusC FileServlet",
as an abstract template, slightly refactored, rewritten and modernized with a.o. fast NIO stuff instead of legacy
RandomAccessFile. GZIP support is stripped off as that can be done application wide via GzipResponseFilter
.
This servlet properly deals with ETag
, If-None-Match
and If-Modified-Since
caching requests, hereby improving browser caching. This servlet also properly deals with Range
and
If-Range
ranging requests (RFC7233), which is required
by most media players for proper audio/video streaming, and by webbrowsers and for a proper resume of an paused
download, and by download accelerators to be able to request smaller parts simultaneously. This servlet is ideal when
you have large files like media files placed outside the web application and you can't use the default servlet.
Just extend this class and override the getFile(HttpServletRequest)
method to return the desired file. If
you want to trigger a HTTP 400 "Bad Request" error, simply throw IllegalArgumentException
. If you want to
trigger a HTTP 404 "Not Found" error, simply return null
, or a non-existent file.
Here's a concrete example which serves it via an URL like /media/foo.ext
:
@WebServlet("/media/*") public class MediaFileServlet extends FileServlet { private File folder; @Override public void init() throws ServletException { folder = new File("/var/webapp/media"); } @Override protected File getFile(HttpServletRequest request) throws IllegalArgumentException { String pathInfo = request.getPathInfo(); if (pathInfo == null || pathInfo.isEmpty() || "/".equals(pathInfo)) { throw new IllegalArgumentException(); } return new File(folder, pathInfo); } }
You can embed it in e.g. HTML5 video tag as below:
<video src="#{request.contextPath}/media/video.mp4" controls="controls" />
FileServlet
If more fine grained control is desired for determining the cache expire time, the content type and whether the file should be supplied as an attachment, then the developer can opt to override one or more of the following protected methods:
getExpireTime(HttpServletRequest, File)
getContentType(HttpServletRequest, File)
isAttachment(HttpServletRequest, String)
See also:
Constructor and Description |
---|
FileServlet() |
Modifier and Type | Method and Description |
---|---|
protected void |
doGet(HttpServletRequest request,
HttpServletResponse response) |
protected void |
doHead(HttpServletRequest request,
HttpServletResponse response) |
protected String |
getContentType(HttpServletRequest request,
File file)
Returns the content type associated with the given HTTP servlet request and file.
|
protected long |
getExpireTime(HttpServletRequest request,
File file)
Returns how long the resource may be cached by the client before it expires, in seconds.
|
protected abstract File |
getFile(HttpServletRequest request)
Returns the file associated with the given HTTP servlet request.
|
protected boolean |
isAttachment(HttpServletRequest request,
String contentType)
Returns
true if we must force a "Save As" dialog based on the given HTTP servlet request and content
type as obtained from getContentType(HttpServletRequest, File) . |
doDelete, doOptions, doPost, doPut, doTrace, getLastModified, service, service
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, init, log, log
protected void doHead(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
doHead
in class HttpServlet
ServletException
IOException
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
doGet
in class HttpServlet
ServletException
IOException
protected abstract File getFile(HttpServletRequest request) throws IllegalArgumentException
null
, or if
File.isFile()
returns false
, then the servlet will then return a HTTP 404 error. If this
method throws IllegalArgumentException
, then the servlet will return a HTTP 400 error.request
- The involved HTTP servlet request.IllegalArgumentException
- When the request is mangled in such way that it's not recognizable as a valid
file request. The servlet will then return a HTTP 400 error.protected long getExpireTime(HttpServletRequest request, File file)
The default implementation returns 30 days in seconds.
request
- The involved HTTP servlet request.file
- The involved file.protected String getContentType(HttpServletRequest request, File file)
The default implementation delegates File.getName()
to ServletContext.getMimeType(String)
with a
fallback default value of application/octet-stream
.
request
- The involved HTTP servlet request.file
- The involved file.protected boolean isAttachment(HttpServletRequest request, String contentType)
true
if we must force a "Save As" dialog based on the given HTTP servlet request and content
type as obtained from getContentType(HttpServletRequest, File)
.
The default implementation will return true
if the content type does not start with
text
or image
, and the Accept
request header is either null
or does not match the given content type.
request
- The involved HTTP servlet request.contentType
- The content type of the involved file.true
if we must force a "Save As" dialog based on the given HTTP servlet request and content
type.Copyright © 2012–2015 OmniFaces. All rights reserved.