@RequestMapping("/download")
public void download(String sfId, HttpServletRequest request, HttpServletResponse response){
try {
if (!StringUtil.isNullOrEmpty(sfId)) {
SampleDocument sampleDocument = sampleDocumentService.findBySfId(sfId);
String title = sampleDocument.getTitle();
Blob content = sampleDocument.getContent();
if (content != null) {
byte[] subByte = content.getBytes(1,(int)content.length());
response.setHeader("Content-lenth", Integer.toString(subByte.length));
// 文件名称转码
response.setHeader("Content-disposition","attachment;filename="+ new String(title.getBytes("GBK"),"ISO-8859-1"));
response.getOutputStream().write(subByte);
response.setStatus(HttpStatus.OK.value());
}
}
} catch (Exception e) {
log.error("download error message:" + e.getMessage(), e);
}
}【SpringMVC】下载功能
最新推荐文章于 2023-11-27 08:48:21 发布
本文详细阐述了如何使用Spring MVC实现文件下载功能,包括文件查找、内容读取、响应头设置以及文件名称转码等关键步骤。
223

被折叠的 条评论
为什么被折叠?



