干货,拿去直接能运行的,亲测有效(测试用的txt文件)
// 读取MultipartFile文件内容 content就是文件内容
public String getJson(MultipartFile file) {
String content = "";
try {
byte[] data = file.getBytes();
content = new String(data, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
// 读取File文件内容 content就是文件内容
public String getJson() {
StringBuffer content = new StringBuffer("");
try {
File file = new File("此处是文件路径");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
content .append(line);
}
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
return content .toString();
}