String name = request.getParameter(PARAM_NAME);
if (StringUtils.isBlank(name)) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
DbFile file = dbFileMng.findById(name);
if (file == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
String mimeType = getServletContext().getMimeType(name);
if (mimeType != null) {
response.setContentType(mimeType);
}
String filename = file.getId();
int index = filename.lastIndexOf("/");
if (index != -1) {
filename = filename.substring(index + 1);
}
response.addHeader("Content-disposition", "filename=" + filename);
response.setContentLength(file.getLength());
ServletOutputStream out = response.getOutputStream();
out.write(file.getContent());
out.flush();
out.close();
}