springmvc ResponseEntity 下载文件损坏问题解决方法

这两天做一个文件下载功能,基本上使用的就是springmvc的ResponseEntity来做的,这块代码就不贴了,网上大把大把的,问题是下载的文件总是提示【文件损坏】导致打不开,word,excel,zip文件统统打不开,研究来研究去网上答案很多但是都不解决问题,唯一解决问题的就是这个帖子,地址如下:http://www.iteye.com/topic/1125784,作者解决问题的思路也很棒。

好了,不多说了,直接说说问题解决方法吧,主要就是配置文件里的顺序:

<bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
                <bean
                    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/json;charset=UTF-8</value>
                            <value>text/json;charset=UTF-8</value>
                            <value>text/html;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/plain;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
                
            </list>
        </property>
    </bean>


红色部分要放在MappingJackson2HttpMessageConverter的前面,修改之后问题解决!


版权声明:本文为u013555226原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。