天天看点

【SpringMVC】下载功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/inforstack/article/details/46727885

@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);
	}
}           

继续阅读