Created
October 1, 2019 22:12
-
-
Save seco/bbfd6f5f9f12506ce74c8ecd29a0e945 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File file = (File) model.get("downloadFile"); | |
String fileName = (String) model.get("fileName"); | |
String header = request.getHeader("User-Agent"); | |
if (header.contains("MSIE") || header.contains("Trident")) { | |
fileName = URLEncoder.encode(fileName,"UTF-8").replaceAll("\\+", "%20"); | |
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ";"); | |
} else { | |
fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1"); | |
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); | |
} | |
response.setContentType(getContentType()); | |
response.setContentLength((int) file.length()); | |
response.setHeader("Content-Type", "application/octet-stream"); | |
response.setContentLength((int)file.length()); | |
response.setHeader("Content-Transfer-Encoding", "binary;"); | |
response.setHeader("Pragma", "no-cache;"); | |
response.setHeader("Expires", "-1;"); | |
OutputStream out = response.getOutputStream(); | |
FileInputStream fis = null; | |
try { | |
fis = new FileInputStream(file); | |
FileCopyUtils.copy(fis, out); | |
} catch (Exception e) { | |
logger.error(e.getMessage()); | |
} finally { | |
if (fis != null) { | |
try { | |
fis.close(); | |
} catch (Exception e) { | |
logger.error(e.getMessage()); | |
} | |
} | |
} | |
out.flush(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment