工具类
/**
* 读取文件内容
*/
public class FileUtils {
/**
* 读取文件内容
* @param path 文件地址
* @return String类型
*/
public static String readFile(String path){
StringBuffer stringBuffer=new StringBuffer();
ClassPathResource classPathResource=new ClassPathResource(path);
try {
InputStream stream=classPathResource.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(stream,"UTF-8"));
String line;
while ((line=br.readLine())!=null){
stringBuffer.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return String.valueOf(stringBuffer);
}
调用(我的publicKey.txt放在resources下)
FileUtils.readFile("publicKey.txt");
这种方式不管本地还是放在服务器上都能找到文件,如果不用流的形式我试的都不行来着