SpringBoot中jar啟動下如何讀取文件路徑
SpringBoot jar啟動下讀取文件路徑
由于我們經(jīng)常使用jar 包作為我們的項目啟動方式 以及我們經(jīng)常會設(shè)涉及到生成文件這時候就需要一個文件路勁存放臨時文件 因為我們正在存放可以在第三方服務(wù)器或者自己文件服務(wù)器。
下面就介紹一種jar 下生成文件存放示例。
代碼如下
@GetMapping("/index") public String getFile() throws IOException { try { File path = new File(ResourceUtils.getURL("classpath:").getPath()); if (!path.exists()) { path = new File(""); System.err.println("path" + path.getAbsolutePath()); } File upload = new File(path.getAbsolutePath(), "static/temp/"); if (!upload.exists()) { boolean mkdirs = upload.mkdirs(); String text = "drj測試"; FileOutputStream fos = new FileOutputStream(upload.getAbsolutePath() +File.separator+ "drj.txt"); fos.write(text.getBytes()); fos.close(); System.err.println("不存在" + mkdirs); } else { System.err.println(upload.getAbsolutePath()); System.err.println("存在"); } return "success"; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "error"; }
截圖如下
最后處理完業(yè)務(wù)邏輯 上傳到自己服務(wù)器 后刪除臨時文件
SpringBoot獲取路徑的方式
前置條件
http://127.0.0.1:9001/aiforce/authentication/sso
1)request.getContextPath()
/aiforce
2)request.getServletPath()
/authentication/sso
只返回傳遞到servlet的路徑
3)request.getPathInfo()
/authentication/sso
只返回傳遞到servlet的路徑
4)request.getRequestURI
/aiforce/authentication/sso
5)request.getRequestURL
http://localhost:9001/aiforce/authentication/sso
返回完整路徑
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java大數(shù)乘法的簡單實現(xiàn) 浮點數(shù)乘法運算
大數(shù)乘法可以進行任意大小和精度的整數(shù)和浮點數(shù)的乘法運算, 精確度很高, 可以用作經(jīng)融等領(lǐng)域的計算,這個是我看了一些資料, 然后自己整理實現(xiàn)的,簡單測試了一下2014-01-01