java ssm学习中的日常问题小日记

来源:互联网 发布:大数据算法设计与分析 编辑:程序博客网 时间:2024/06/05 11:58

//controller中还是要主语文件名,切记将文件类型传递过来

 @RequestMapping("/download")    public ResponseEntity<byte[]> download(HttpServletRequest request, HttpServletResponse response,                                           @RequestParam("fileName") String fileName) throws Exception{        String path = request.getServletContext().getRealPath("/uploadFile/");        File thefile = new File(path+File.separator+fileName);            //        下载显示文件名,为了解决中文名称乱码            HttpHeaders httpHeaders = new HttpHeaders();            String downloadFileName = new String (fileName.getBytes("UTF-8"),"iso-8859-1");//        设置下载方式为attachment            httpHeaders.setContentDispositionFormData("attachment",downloadFileName);//        用二进制流的方式下载文件            httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);            return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(thefile),httpHeaders, HttpStatus.CREATED);    }

jsp中

<a href="download?fileName=文件名">文件名</a>
原创粉丝点击