tds/src/main/java/thredds/servlet/ServletUtil.java
public static void returnFile(HttpServlet servlet, HttpServletRequest req, HttpServletResponse res, File file, String contentType)
          throws IOException {

    // No file, nothing to view
    if (file == null) {
      res.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }

    // check that it exists
    if (!file.exists()) {
      res.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }

    // not a directory
    if (!file.isFile()) {
      res.sendError(HttpServletResponse.SC_BAD_REQUEST);
      return;
    }

    // Set the type of the file
    String filename = file.getPath();
    if (null == contentType) {
      if (filename.endsWith(".html"))
        contentType = ContentType.html.getContentHeader();
      else if (filename.endsWith(".xml"))
        contentType = ContentType.xml.getContentHeader();
      else if (filename.endsWith(".txt") || (filename.endsWith(".log")))
        contentType = ContentType.text.getContentHeader();
      else if (filename.indexOf(".log.") > 0)
        contentType = ContentType.text.getContentHeader();
      else if (filename.endsWith(".nc"))
        contentType = ContentType.netcdf.getContentHeader();
      else if (filename.endsWith(".nc4"))
        contentType = ContentType.netcdf.getContentHeader();
      else if (servlet != null)
        contentType = servlet.getServletContext().getMimeType(filename);

      if (contentType == null) contentType = ContentType.binary.getContentHeader();
    }

    returnFile(req, res, file, contentType);
  }
public static void returnFile(HttpServlet servlet, HttpServletRequest req, HttpServletResponse res, File file, String contentType)
          throws IOException {

    // No file, nothing to view
    if (file == null) {
      res.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }

    // check that it exists
    if (!file.exists()) {
      res.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }

    // not a directory
    if (!file.isFile()) {
      res.sendError(HttpServletResponse.SC_BAD_REQUEST);
      return;
    }

    // Set the type of the file
    String filename = file.getPath();
    if (null == contentType) {
      if (filename.endsWith(".html"))
        contentType = ContentType.html.getContentHeader();
      else if (filename.endsWith(".xml"))
        contentType = ContentType.xml.getContentHeader();
      else if (filename.endsWith(".txt") || (filename.endsWith(".log")))
        contentType = ContentType.text.getContentHeader();
      else if (filename.indexOf(".log.") > 0)
        contentType = ContentType.text.getContentHeader();
      else if (filename.endsWith(".nc"))
        contentType = ContentType.netcdf.getContentHeader();
      else if (filename.endsWith(".nc4"))
        contentType = ContentType.netcdf.getContentHeader();
      else if (servlet != null)
        contentType = servlet.getServletContext().getMimeType(filename);

      if (contentType == null) contentType = ContentType.binary.getContentHeader();
    }

    returnFile(req, res, file, contentType);
  }
Write a file to the response stream. Handles Range requests.

@param servlet     called from this servlet, may be null
@param req         the request
@param res         the response
@param file        to serve
@param contentType content type, if null, will try to guess
@throws IOException on write error