DocumentDAO documentDAO;
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
Document document;
try {
document = documentDAO.findByName(name);
if (document == null) {
// Pb
throw new NullPointerException();
} else {
/* Code for download */
String filename = document.getFilename();
String fileType = filename.substring(filename.indexOf(".") + 1, filename.length());
if (fileType.trim().equalsIgnoreCase("txt")) {
response.setContentType("text/plain");
} else if (fileType.trim().equalsIgnoreCase("doc")) {
response.setContentType("application/msword");
} else if (fileType.trim().equalsIgnoreCase("xls")) {
response.setContentType("application/vnd.ms-excel");
} else if (fileType.trim().equalsIgnoreCase("pdf")) {
response.setContentType("application/pdf");
} else if (fileType.trim().equalsIgnoreCase("ppt")) {
response.setContentType("application/ppt");
} else {
response.setContentType("application/octet-stream");
}
response.setHeader("Content-Disposition", "buffer; filename=\"" + filename + "\"");
response.setHeader("cache-control", "no-cache");
byte[] fileBytes = document.getBuffer();
ServletOutputStream outs = response.getOutputStream();
outs.write(fileBytes);
outs.flush();
outs.close();