public void downloadFile(HttpServletResponse response, String filename, String filenameToDisplay) {
try (
FileInputStream inputStream = new FileInputStream(filename);
OutputStream outputStream = response.getOutputStream()
) {
response.setContentType("application/octet-stream");
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filenameToDisplay, "UTF-8"));
int length;
byte[] buffer = new byte[1024];
while ((length = inputStream.read(buffer)) != -1)
outputStream.write(buffer, 0, length);
} catch (IOException e) {
throw new FileNotDownloadedException();
}
}