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

举报
yd_273762914 发表于 2020/12/02 22:29:38 2020/12/02
【摘要】 springMVC系列之@Responsebody接口弹出下载页面问题 最近遇到一个文件上传接口,调用时候出现f.txt下载问题,这个估计很多人都有遇到过,网上找资料,很多博客都是说用如下类似代码: <mvc:annotation-driven> <mvc:message-converters> <bean class="or...

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

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

<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>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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

接口代码如:

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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

文章来源: smilenicky.blog.csdn.net,作者:smileNicky,版权归原作者所有,如需转载,请联系作者。

原文链接:smilenicky.blog.csdn.net/article/details/106408861

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。