private void downloadFile(File targetFile, HttpServletResponse response) {
try {
InputStream inputStream = Files.newInputStream(targetFile.toPath());
response.setContentType("application/octet-stream");
response.setHeader("content-type","application/octet-stream");
response.addHeader("Content-Disposition","attachment;filename=" + targetFile.getName());
OutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int b = 0;
while ((b = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, b);
}
inputStream.close();
if (out != null) {
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}