Spring boot访问resource下的文件

xiaoxiao2021-03-01  2

File file = ResourceUtils.getFile("classpath:test"); //test是resource下的文件下文件夹 if(file.exists()){ File[] files = file.listFiles(); if(files != null){ for(File childFile:files){ // 处理响应流,必须与服务器响应流输出的编码一致 InputStream inputStream = null; BufferedReader br = null; inputStream = new FileInputStream(childFile); br = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); } System.out.println(sb.toString()); } } }

如果打成jar包运行项目,会存在找不到资源的错误。

解决方法:

ClassPathResource cpr = new ClassPathResource(fileUrl); byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream()); data = new String(bdata, StandardCharsets.UTF_8); return data;

 

转载请注明原文地址: https://www.6miu.com/read-3850085.html

最新回复(0)