避坑:
- 这里不要适用hutool的ClassPathResource
- Ubuntu上用其他方式读取文件似乎会报错,但是流读取的方式不会
public static String getFileContent(String fileName) {
//如果你想获取resources目录下的其他文件,修改下面的"static"即可
org.springframework.core.io.ClassPathResource classPathResource = new org.springframework.core.io.ClassPathResource("static/"+fileName);
// 获取文件内容
InputStream inputStream = classPathResource.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
//byte数组转字符串
String str = new String(bos.toByteArray(), StandardCharsets.UTF_8);
}