前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springMVC系列之@Responsebody接口弹出f.txt下载问题

springMVC系列之@Responsebody接口弹出f.txt下载问题

作者头像
SmileNicky
发布2020-05-29 15:54:41
3810
发布2020-05-29 15:54:41
举报
文章被收录于专栏:Nicky's blog

springMVC系列之@Responsebody接口弹出下载页面问题

最近遇到一个文件上传接口,调用时候出现f.txt下载问题,这个估计很多人都有遇到过,网上找资料,很多博客都是说用如下类似代码:

代码语言:javascript
复制
<mvc:annotation-driven>
		 <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                 <property name="supportedMediaTypes">  
		            <list>  
		                <value>text/json;charset=UTF-8</value>  
		                <value>text/html;charset=UTF-8</value>  
		                <value>application/json;charset=UTF-8</value>  
		            </list>  
		        </property>    
            </bean>
   		 </mvc:message-converters>
	</mvc:annotation-driven>

反正基本大同小异,不过我测试过,在ie,360极速浏览器都有问题,Spring的版本是4.2.2.RELEASE

接口代码如:

代码语言:javascript
复制
@RequestMapping("/updateHandInfo")
	@ResponseBody
	public ResultModel updateHandInfo(@RequestParam(value = "file", required = false) MultipartFile file,HttpServletRequest request,
			HandleDto handleDto)throws Exception{
		try {
			...
			return new ResultModel(true,"签收成功",resultMap);
		} catch (Exception e) {
			logger.error("签收失败",e);
			return new ResultModel(false,"签收失败",null);
		}
	}

用网上的方法没解决问题,只能改变一下了,用response的方法,代码改造如:

代码语言:javascript
复制
@RequestMapping("/updateHandInfo")
	//@ResponseBody
	public void updateHandInfo(@RequestParam(value = "file", required = false) MultipartFile file,HttpServletRequest request,
			HandleDto handleDto,,HttpServletResponse response)throws Exception{
			String jsonStr = "";
		try {
			...
			jsonStr = JSONObject.toJSONString(new ResultModel(true,"签收成功",resultMap));
		} catch (Exception e) {
			logger.error("签收失败",e);
			jsonStr =  JSONObject.toJSONString(new ResultModel(false,"签收失败","0"));
		}
		// fix bug 直接通过response返回
		this.toJson(response, jsonStr);
	}

protected void toJson(HttpServletResponse response,String jsonString) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().write(jsonString);
	}

ResultModel 是封装的Model,这种方法虽然比较麻烦点,不过是可以解决问题的,所以本博客记录起来,仅供互相学习参考

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/05/28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • springMVC系列之@Responsebody接口弹出下载页面问题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档