IE中返回application/json弹出下载框问题
来源:互联网 发布:db2 oracle mysql 编辑:程序博客网 时间:2024/06/10 08:21
js 配合java springMVC后台,成功后返回消息,chrom ff都正常,只有IE提交后返回的JSON弹出下载框
网上一般有三种方法:
- 一:是手工指定response
- 二:是修改配置文件(经过修改,会出现左上角弹出一个连接框)
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters></mvc:annotation-driven>
- 三:是不再用 @ResponseBody,改用ResponseEntity
这种方法成功率最高,但是修改起来比较麻烦,如果你有很多地方已经用了@ResponseBody的话
第三种参考下面博客
http://blog.csdn.net/gao36951/article/details/46785323
- 四:修改配置文件,定义一个转化器
具体配置如下
<mvc:annotation-driven> <mvc:message-converters> <bean class="com.test.core.Utf8StringHttpMessageConverter" /> </mvc:message-converters> </mvc:annotation-driven>
Utf8StringHttpMessageConverter源码如下,在writeInternal方法中添加对IE浏览器的判断处理逻辑
if(outputMessage.getHeaders().getContentType(). getSubtype().equals("x-ms-application")){ //针对IE的兼容性处理,统一转化为text/plain outputMessage.getHeaders().setContentType(MediaType.TEXT_PLAIN);}
package com.test.core;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.List;import org.springframework.http.HttpInputMessage;import org.springframework.http.HttpOutputMessage;import org.springframework.http.MediaType;import org.springframework.http.converter.AbstractHttpMessageConverter;import org.springframework.util.FileCopyUtils;public class Utf8StringHttpMessageConverter extends AbstractHttpMessageConverter<String> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private final Charset defaultCharset; private final List<Charset> availableCharsets; private boolean writeAcceptCharset = true; public Utf8StringHttpMessageConverter() { this(DEFAULT_CHARSET); } public Utf8StringHttpMessageConverter(Charset defaultCharset) { super(new MediaType("text", "plain", defaultCharset), MediaType.ALL); this.defaultCharset = defaultCharset; this.availableCharsets = new ArrayList<Charset>(Charset .availableCharsets().values()); } public void setWriteAcceptCharset(boolean writeAcceptCharset) { this.writeAcceptCharset = writeAcceptCharset; } @Override public boolean supports(Class<?> clazz) { return String.class.equals(clazz); } @Override protected String readInternal(@SuppressWarnings("rawtypes") Class clazz, HttpInputMessage inputMessage) throws IOException { Charset charset = getContentTypeCharset(inputMessage.getHeaders() .getContentType()); return FileCopyUtils.copyToString(new InputStreamReader(inputMessage .getBody(), charset)); } @Override protected Long getContentLength(String s, MediaType contentType) { Charset charset = getContentTypeCharset(contentType); try { return (long) s.getBytes(charset.name()).length; } catch (UnsupportedEncodingException ex) { throw new IllegalStateException(ex); } } @Override protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException { if (this.writeAcceptCharset) { outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets()); if(outputMessage.getHeaders().getContentType().getSubtype().equals("x-ms-application")){//针对IE的兼容性处理,统一转化为text/plain outputMessage.getHeaders().setContentType(MediaType.TEXT_PLAIN); } } Charset charset = getContentTypeCharset(outputMessage.getHeaders() .getContentType()); FileCopyUtils.copy(s, new OutputStreamWriter(outputMessage.getBody(), charset)); } protected List<Charset> getAcceptedCharsets() { return this.availableCharsets; } private Charset getContentTypeCharset(MediaType contentType) { if (contentType != null && contentType.getCharSet() != null) { return contentType.getCharSet(); } else { return this.defaultCharset; } }}
这样就解决了编码问题和IE弹出下载框的问题,如有其他更好的方法欢迎赐教
0 0
- IE中返回application/json弹出下载框问题
- servlet返回application/json数据类型在ie浏览器中提示下载文件问题
- IE浏览器请求json数据弹出下载框问题
- 服务器返回JSON,IE出现下载问题
- IE下ajax返回值为json弹出下载框的方法总结
- IE使用ajaxFileUpload上传文件后台返回json类型前台弹出下载提示框
- IE浏览器请求json数据弹出下载框问题【在springmvc中配置如下内容,可以避免】
- 表单提交返回json 弹出下载框
- 解决EasyUI Form submit和SpringMVC返回JSON在IE中变下载的问题
- 解决SpringMVC返回JSON在IE中变下载的问题
- 解决SpringMVC返回JSON在IE中变下载的问题
- Jquery 返回json数据,IE浏览器提示下载的问题
- controller返回json到页面,IE提示下载问题
- SpringMVC返回JSON , 解决IE下下载问题
- springmvc+easyui Ajax提交返回json IE出现下载问题
- 从http协议研究json返回在IE中下载
- 解决struts2 返回json是,在ie,360等浏览器中弹出下载框的问题。
- .NET easyui form提交后,返回json数据在IE下弹出下载的问题解决方法
- TDBGrid 数据导入到execle
- Codevs_P2800 送外卖(状态压缩DP+Floyd)
- Oracle 级联更新多表
- Oracle 去掉回车、换行、空格操作
- 线程:This application is modifying the autolayout engin from a background thread
- IE中返回application/json弹出下载框问题
- TRUNCATE TABLE 与 DELETE在删除整个表的所有记录时的区别具体到性能,效率,操作方式等方面
- Oracle 设置回滚保留时间
- 非Activity类中调用Activity方法
- destoon中input框为什么需要隐藏?
- 2016蓝桥杯基础训练——特殊的数字
- Oracle 存储过程 drop后闪回恢复
- Oracle 查看当前数据库最大连接数
- html为什么需要采用表单提交数据