@RequestMapping(value = "download", method = RequestMethod.GET)
public String download(
@RequestParam(value = "questionId") String question,
HttpServletResponse response, HttpServletRequest request) {
QuestionDto questionDto = null;
try {
Integer questionIdentifier = Integer.parseInt(question);
questionDto = this.userHelpAndSupportService.getQuestionById(questionIdentifier);
String filename1 = questionDto.getAttachmentName();
if (filename1.contains(".doc") || filename1.contains(".docx")) {
response.setContentType("application/vnd.ms-word");
response.setHeader("Content-Disposition",
"attachment;filename=" + filename1);
} else if (filename1.contains(".xls")
|| filename1.contains(".xlsx")) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + filename1);
} else if (filename1.contains(".pdf")) {
response.setContentType("application/pdf");
response.setHeader("Content-Disposition",
"attachment;filename=" + filename1);
} else if (filename1.contains(".txt")) {
response.setContentType("application/txt");
response.setHeader("Content-Disposition",
"attachment;filename=" + filename1);
}
OutputStream out = response.getOutputStream();
FileCopyUtils.copy(questionDto.getAttachment(), out);
out.flush();
out.close();
} catch (Exception e) {
if (questionDto != AdPlannerAdminConstants.NULL){
PlatformLoggerUtil.info(this.getClass(),"The filename to be downloaded is "+questionDto.getAttachmentName());
PlatformLoggerUtil.error(this.getClass(),"Exception in AdminHelpAndSupportController:download "+e);
}
}
return null;
}
每当我尝试下载文件时,在mozilla和chrome中,文件的扩展名都会针对某些文件进行更改。下载的文件不可读。如何解决这个问题?