spring-mvc - 关于SpringMvc下载文件时自定义文件名的问题

查看:109
本文介绍了spring-mvc - 关于SpringMvc下载文件时自定义文件名的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

是这样的,我们的项目用的框架是SpringMvc+Spring+Mybatis,其中有一个下载文件的功能,在下载下来的时候需要对其自定义文件名,我们在本地测试这个功能毫无问题,试了几台电脑都行,然后我们把它发布到服务器上去,服务器环境是Tomcat7.0+Jdk1.8和本地开发环境一样,但是测试下载功能的时候,下载下来的文件的文件名就变成了download

这是本地测试的情况,文件名和显示的文件名一样

这是服务上下载的情况,显示的文件名就是download



这是控制层的代码

    @RequestMapping(value="/download",method={RequestMethod.GET,RequestMethod.POST})
    public ResponseEntity<byte []> downloadFiles(String f0901,HttpServletRequest req,HttpServletResponse resp) {
        
        T09 t09 = lowCardService.selectLowByPrimaryKey(Integer.parseInt(f0901));
        String filenameIdname = t09.getF0903();
        String filename=t09.getF0902();
        ResponseEntity<byte []> rr;
        resp.reset();
        try {
            //传入文件UUID形成的文件名filenameIdname,再加入本件本身的名字filename
            rr=Utils.downloadfile(filenameIdname,filename,req,resp);
            return rr;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }    
    }

这是实现下载的代码
` public static ResponseEntity<byte []> downloadfile(String filenameIdname,String filename,HttpServletRequest req,HttpServletResponse resp) {

    
    //下载文件路径
    String path = req.getServletContext().getRealPath("/")+"file/";
    File file = new File(path+filenameIdname);
    HttpHeaders headers = new HttpHeaders();
    
    try {
        //下载显示的文件名,解决文件乱码问题
        String downlaodFilename = URLEncoder.encode(filename); //new String(filename.getBytes("UTF-8"), "iso-8859-1");
        
        //通知浏览器以attachment(下载方式)
        headers.setContentDispositionFormData("attachment", downlaodFilename);
        //二进制流数据
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        System.out.println("-11111");
        resp.setHeader("Content-Disposition", "attachment;fileName="
                + downlaodFilename);
        return new ResponseEntity<byte []>(FileUtils.readFileToByteArray(file), headers,HttpStatus.CREATED);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return null;
    }    
}`

这是SpringMvc的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">

    <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
    <context:component-scan base-package="com.wh.controller" />

    <mvc:annotation-driven>
    </mvc:annotation-driven>

    <mvc:default-servlet-handler />

    <mvc:annotation-driven>
        <!-- 处理responseBody 里面日期类型 -->
        <mvc:message-converters>
            <bean
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                        <property name="dateFormat">
                            <bean class="java.text.SimpleDateFormat">
                                <constructor-arg type="java.lang.String" value="yyyy-MM-dd" />
                            </bean>
                        </property>
                        <property name="timeZone">
                            <bean class="java.util.TimeZone" factory-method="getTimeZone">
                                <constructor-arg value="GMT+08" />
                            </bean>
                        </property>
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <!--配置下载返回类型 -->
                <bean
                    class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
                
            <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" >  
                <property name = "supportedMediaTypes">  
                    <list>  
                        <value>text/plain;charset=UTF-8</value>  
                    </list>  
                </property>  
            </bean>
            </list>
        </property>
    </bean>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding">
            <value>UTF-8</value>
        </property>
        <property name="maxUploadSize">
            <value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
        </property>
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
    </bean>

</beans>

本地测试正常,服务器上就这样,真的不知道是什么原因了,请各位大佬帮忙看看,小弟先谢谢各位了

请输入代码

解决方案

看我的这篇文章试试

这篇关于spring-mvc - 关于SpringMvc下载文件时自定义文件名的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆