【SpringMVC】下载功能

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/inforstack/article/details/46727885 ...
版权声明:本文为博主原创文章,未经博主允许不得转载。 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);
	}
}

相关文章
|
2月前
|
缓存 前端开发 Java
Spring MVC 面试题及答案整理,最新面试题
Spring MVC 面试题及答案整理,最新面试题
101 0
|
2月前
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
11 0
|
2月前
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
10 0
|
2月前
|
SQL JavaScript Java
springboot+springm vc+mybatis实现增删改查案例!
springboot+springm vc+mybatis实现增删改查案例!
27 0
|
2月前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
57 1
|
2天前
|
监控 前端开发 Java
SpringBoot与SpringMVC有哪些区别?
SpringBoot和SpringMVC是Java开发中常用的两个框架,它们都是由Spring框架所提供的,但在功能和使用方式上有着一些区别。
12 2
|
25天前
|
数据采集 前端开发 Java
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
23 3
|
25天前
|
存储 前端开发 Java
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
14 1
|
25天前
|
前端开发 Java Spring
数据之桥:深入Spring MVC中传递数据给视图的实用指南
数据之桥:深入Spring MVC中传递数据给视图的实用指南
32 3
|
1月前
|
前端开发 安全 Java
使用Java Web框架:Spring MVC的全面指南
【4月更文挑战第3天】Spring MVC是Spring框架的一部分,用于构建高效、模块化的Web应用。它基于MVC模式,支持多种视图技术。核心概念包括DispatcherServlet(前端控制器)、HandlerMapping(请求映射)、Controller(处理请求)、ViewResolver(视图解析)和ModelAndView(模型和视图容器)。开发流程涉及配置DispatcherServlet、定义Controller、创建View、处理数据、绑定模型和异常处理。
使用Java Web框架:Spring MVC的全面指南