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