项目在google浏览器下都很nice了,但当测试到IE的时候开始出现各类问题。前端
项目是前端js经过URL传参fileName到后台解析返回ResponseEntity浏览器
前端代码以下:tomcat
window.location.href="downPlan.do?fileName=fileName;
后台代码:app
@RequestMapping({"/downPlan.do"}) //@ResponseBody public ResponseEntity<byte[]> downPlan(HttpServletRequest request, @RequestParam("fileName") String fileName) throws Exception { String path = "C:/check/plan/"; String fName =fileName+".xlsx"; File file = new File(path + File.separator + fName); if(!file.exists()){ fName =fileName+".xls"; file = new File(path + File.separator + fName); } HttpHeaders headers = new HttpHeaders(); String downloadFielName = new String(fName.getBytes("UTF-8"), "iso-8859-1"); headers.setContentDispositionFormData("attachment",downloadFielName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}
一、测试IE9下,出现错误:The valid characters are defined in RFC 7230 and RFC 3986,百度缘由好像是URL中包含了超出RFC 7230和RFC 3968所定义的字符测试
解决方法:能够换tomcat的版本,过于麻烦google
或者修改tomcat配置文件编码
我选了个最简单的编码 - 反编码spa
前端修改为code
window.location.href="downPlan.do?fileName="+encodeURI(encodeURI(fileName));
后台反编码便可orm
fileName = URLDecoder.decode(fileName,"UTF-8");
二、IE下载文件异常,文件名是URL的地址
异常以下图:
废话很少说:HttpStatus.CREATED改成HttpStatus.OK便可以,由于IE不支持201状态
return new ResponseEntity(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
三、下载文件会出现中文乱码
文件能够正常下载,可是出现乱码就很烦了。
google能够正常显示,IE则会出现乱码,header中只支持ASCII因此咱们传输的文件名必须是ASCII,
String downloadFielName = new String(fName.getBytes("UTF-8"), "iso-8859-1");
google能够自动识别编码方式,会对其进行反解码,而IE则只会用GBK的方法,因此IE下载文件名仍是乱码,改为如下:
String downloadFielName = new String(fName.getBytes("GBK"), "iso-8859-1");
把UTF-8改为GBK便可
详情参考大神的博客:https://yq.aliyun.com/articles/38945