我正在从控制器下载文件。这是行不通的。这是因为文件名中的“,”。如果我从名称中删除“,”,它将起作用。有没有解决的办法。我应该做String.replace(“,”“”)。肯定有更好的办法?
response.setHeader("Content-Disposition", "inline; filename=" + "abbbbababbaababba.4.1.2013,aakdfhfdf.xlsx");
完整的方法
@RequestMapping(value = "/newsimage/{newsImageId} ", method = RequestMethod.GET)
public void getImage(HttpServletRequest request, HttpServletResponse response, @PathVariable long newsImageId) {
NewsImage newsImage = newsImageService.findById(newsImageId);
String fileName = newsImage.getFileName();
response.setHeader("Content-Disposition", "inline; filename=" + "abbbbababbaababba.4.1.2013,aakdfhfdf.xlsx");
response.setContentType(newsImage.getContentType());
// response.setHeader("Content-Disposition", "attachment;filename=" + newsImage.getFileName());
OutputStream out;
try {
out = response.getOutputStream();
out.write(newsImage.getData());
out.flush();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
最佳答案
您需要引用文件名,否则将其解释为标题属性的一部分。
response.setHeader("Content-Disposition",
"inline; filename=\"" + "abbbbababbaababba.4.1.2013,aakdfhfdf.xlsx" + "\"");