ResponseEntity 文件下载 在IE浏览器报错,其他浏览器正常

//后台代码
@RequestMapping(value="/fileDownMeth",produces = "application/octet-stream;charset=UTF-8") public ResponseEntity<byte[]> download(HttpServletRequest request, @RequestParam(required=false) String url, @RequestParam(required=false) String filename, Model model)throws Exception {//下载文件路径 String paths=SystemPath.getUploadPath(); url = paths +"/"+ url; File file = new File(url); //下载显示的文件名,解决中文名称乱码问题 String downloadFielName = new String(filename.getBytes("UTF-8"),"iso-8859-1"); String agent = request.getHeader("User-Agent").toUpperCase(); boolean isMSIE = ((agent != null && agent.indexOf("MSIE") != -1 ) || ( null != agent && -1 != agent.indexOf("LIKE GECKO"))); HttpHeaders headers = new HttpHeaders(); //application/octet-stream : 二进制流数据(最常见的文件下载)。 headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); if(isMSIE) {//IE
        //解决IE浏览器 下载会出现文件名中文乱码
downloadFielName = new String(filename.getBytes("GBK"), "iso-8859-1"); headers.setContentDispositionFormData("attachment", downloadFielName); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK); } headers.setContentDispositionFormData("attachment", downloadFielName); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); }

下图是因为IE不支持201状态,所以判断浏览器类型 为IE浏览器走if(isMSIE)中的返回

//前台方法
//
下载上传的文件 urls:文件路径;fName:文件名称 var downFiles = function (urls,fName){ var fURI= decodeURI(urls); fName=decodeURI(fName); window.location.href=ctx+"/sys/filedownblock/fileDownMeth?url="+encodeURI(encodeURI(fURI))+"&filename="+encodeURI(encodeURI(fName)); }
原文地址:https://www.cnblogs.com/ljc1212/p/14338482.html