我的springboot项目中的resource静态资源有一个文件novl.txt文件,我在Windows下运行可以对其进行读取并且写入内容,但是当我打成jar之后,在centos服务器中,就无法读取到改文件,只能改为通过用getResourceAsStream的方法拿到输入流,但是这样这能读取,我如何进行写入呢?
3条回答 默认 最新
- 伍六七AI编程 2021-08-26 13:58关注
打包之后,文件路径就变了
需要读入流,然后把流写入临时文件// templateFilePath 文件相对路径 public void setTxtResource(String templateFilePath) { ClassPathResource imgResource = new ClassPathResource(templateFilePath); try (InputStream inputStream = imgResource.getInputStream()) { // 正文模板中的图片 File imageFile = File.createTempFile("文件名", ".pdf");// 文件名;文件后缀 FileUtils.copyInputStreamToFile(inputStream, imageFile); } catch (IOException e) { log.error(""); e.printStackTrace(); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报