重点就在于添加 "attachment;filename*=utf-8'zh_cn'" + fileName
//遇到的现象是,下载含有中文文件名的文件时,能获取到文件,但是使用IE正常,使用firefox,chrome文件名却乱码. //既然如此,就区分一下浏览器再返回好了,处理方式如下 //RESTfull @RequestMapping(path = { "/down/{fileId}" }, method = { RequestMethod.GET }) public ResponseEntity<byte[]> handlerDownload(@PathVariable String fileId,HttpServletRequest request, HttpServletResponse response) throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); FileInfo fileInfo = this.fileManager.fileInfoHandler(fileId); byte[] fileByte = null; if(fileInfo !=null){ /**文件下载图片乱码处理**/ String fileName = URLEncoder.encode(fileInfo.getcFReName(), "UTF-8"); if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {//1.IE浏览器UTF-8 headers.setContentDispositionFormData("attachment", fileName); }else{//2.其他浏览器attachment;filename*=utf-8'zh_cn response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + fileName);//重点 } fileByte = this.fileManager.fetchFile(fileInfo); } return new ResponseEntity<byte[]>(fileByte , headers, HttpStatus.OK); }
ie乱码和正常对比图
firefox乱码和正常对比图