1、出现问题:代码在idea启动测试没有问题,部署到tomcat中下载文件错误
2、原因:tomcat下获取当前路径的url中含有空C:\Program%20Files\Apache%20Software%20Foundation\Tomcat
3、解决方法:
//获取配置文件路径 String filePath = this.getClass().getClassLoader().getResource("xxx/xx.txt").getPath();
(1)//去掉空格
filePath = filePath.replace("%20", " ");
(2)转码
filePath = URLDecoder.decode(filePath,"UTF-8");
4、用处理的url重新下载
File newFile= new File(filePath); //文件存在进行下载 if (newFile.exists()) { try { downloadFile(filePath, response); return "下载成功"; } catch (IOException e) { e.printStackTrace(); } }