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) { 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 handling "file not found" error, determining the cache expire time, the content type, whether the file should be supplied as an attachment and the attachment's file name, then the developer can opt to override one or more of the following protected methods:
handleFileNotFound(HttpServletRequest, HttpServletResponse)
getExpireTime(HttpServletRequest, File)
getContentType(HttpServletRequest, File)
isAttachment(HttpServletRequest, String)
getAttachmentName(HttpServletRequest, File)
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 |
getAttachmentName(HttpServletRequest request,
File file)
Returns the file name to be used in
Content-Disposition header. |
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 void |
handleFileNotFound(HttpServletRequest request,
HttpServletResponse response)
Handles the case when the file is not found.
|
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)
IllegalArgumentException
, then the servlet will return a HTTP 400 error.
If this method returns null
, or if File.isFile()
returns false
, then the
servlet will invoke handleFileNotFound(HttpServletRequest, HttpServletResponse)
.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 void handleFileNotFound(HttpServletRequest request, HttpServletResponse response) throws IOException
The default implementation sends a HTTP 404 error.
request
- The involved HTTP servlet request.response
- The involved HTTP servlet response.IOException
- When something fails at I/O level.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.protected String getAttachmentName(HttpServletRequest request, File file)
Content-Disposition
header.
This does not need to be URL-encoded as this will be taken care of.
The default implementation returns File.getName()
.
request
- The involved HTTP servlet request.file
- The involved file.Content-Disposition
header.Copyright © 2012–2018 OmniFaces. All rights reserved.