首页 > 编程语言 > 详细

springmvc实现文件下载

时间:2018-05-04 10:08:01      阅读:181      评论:0      收藏:0      [点我收藏+]
//在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实现文件下载

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!