File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX+"static/detect.js");
String result = IOUtils.toString(new FileInputStream(file), String.valueOf(StandardCharsets.UTF_8));
上面的方法也行,但是打包後,會有
cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/suning-service-api.jar!/BOOT-INF/classes!/static/detect.js 的錯誤
還有另外一種方式
ClassPathResource resource = new ClassPathResource("static/detect.js");
InputStream inputStream = resource.getInputStream();
String result = IOUtils.toString(inputStream, String.valueOf(StandardCharsets.UTF_8));
從流中讀取一行
//讀取一行
IOUtils.readLines(inputStream).forEach(System.out::println);