SpringBoot读取resources目录下文件
2021-12-31 本文已影响0人
AC编程
/**
* SpringBoot 读取resources目录下文件
* 如读取文件:src/main/resources/alipay/appCertPublicKey.crt
* @return
*/
private String readResourcesFile(){
String path="alipay/appCertPublicKey.crt";
org.springframework.core.io.Resource resource = new ClassPathResource(path);
StringBuffer contentBuffer = new StringBuffer();
InputStream inputStream = null;
InputStreamReader streamReader = null;
BufferedReader bufferedReader = null;
String content;
try{
inputStream = resource.getInputStream();
streamReader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(streamReader);
while((content = bufferedReader.readLine()) != null) {
contentBuffer.append(content+"\n");
}
log.info(path+":content:"+contentBuffer.toString());
}catch (Exception e){
e.printStackTrace();
}finally {
try{
if(bufferedReader!=null){
bufferedReader.close();
}
if(streamReader!=null){
streamReader.close();
}
if(inputStream!=null){
inputStream.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
return contentBuffer.toString();
}