码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc实现文件下载

时间:2018-05-04 10:29:51      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:creat   str   app   pre   except   adf   framework   orm   iso   

//在springmvc中配置
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
               <bean class="org.springframework.http.converter.StringHttpMessageConverter" />


//controller写法
@RequestMapping("download.do")
    public ResponseEntity<byte[]> download(HttpServletRequest request,HttpServletResponse response,String pictureFile) throws Exception{
        File file=new File("F:\\upload\\image\\"+pictureFile);  
        HttpHeaders headers = new HttpHeaders();    
        pictureFile=new String(pictureFile.getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题  
        headers.setContentDispositionFormData("attachment", pictureFile);   
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);   
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
                                          headers, HttpStatus.CREATED);    
        
    }

 

springmvc实现文件下载

标签:creat   str   app   pre   except   adf   framework   orm   iso   

原文地址:https://www.cnblogs.com/ZhangHaiBK/p/8988709.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!