1 public void importText(String osKey, MultipartFile file, String fileName) throws Exception { 2 if (!fileName.endsWith("txt")) { 3 throw new Exception("文件不是txt"); 4 } 5 // 获取字符缓冲流 6 InputStreamReader isr = new InputStreamReader(file.getInputStream()); 7 BufferedReader bf = new BufferedReader(isr); 8 String str; 9 while ((str=bf.readLine()) != null) { 10 // 转化为json对象 11 Object parse = JSON.parse(str); 12 // 存入mongo库 13 mongoTemplate.insert(parse,"user_" + osKey); 14 } 15 }