SpringBoot讀取Linux服務器某路徑下文件\讀取項目Resource下文件


// SpringBoot讀取Linux服務器某路徑下文件
public String messageToRouted() { File file = null; try { file = ResourceUtils.getFile("/home/admin/logs/test/routed.txt"); // 獲取文件輸入流 InputStream inputStream = new FileInputStream(file); List<String> fileList = IOUtils.readLines(inputStream); log.info("fileList:{}", fileList); fileList.forEach(rd -> { RoutedPO routedPO = new RoutedPO(); routedPO.setFulfillmentOrderName(rd.trim()); routedPO.setRoutedTo("test"); // 通知分單結果 routedService.notifyRoutedResult(routedPO, OfcRoutedOperate.INSERT); }); } catch (FileNotFoundException e) { log.info("文件不存在!"); return "文件不存在!"; } catch (IOException e) { log.info("文件讀取異常!"); return "文件讀取異常!"; } return "success!"; }
// SpringBoot讀取Resource下文件
public String messageToRouted(){ try { ClassPathResource classPathResource = new ClassPathResource("routed.txt"); // 判斷文件是否存在 boolean exists = classPathResource.exists(); if(!exists){ log.info("文件不存在!"); return "文件不存在!"; } try { // 獲取文件輸入流 InputStream inputStream = classPathResource.getInputStream(); List<String> fileList = IOUtils.readLines(inputStream); log.info("fileList:{}",fileList); fileList.forEach(rd ->{ RoutedPO routedPO = new RoutedPO(); routedPO.setFulfillmentOrderName(rd.trim()); routedPO.setRoutedTo("test"); // 通知分單結果 routedService.notifyRoutedResult(routedPO, OfcRoutedOperate.INSERT); }); } catch (IOException e) { log.info(e.getMessage()); } } catch (RuntimeException e) { log.info(e.getMessage()); } return "success!"; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM