linux環境下在springboot項目中獲取項目路徑(用於保存文件等)


//application.properties中設置:(file.path=static/qrfile/)
//保存到static文件夾下的qrfile目錄
@Value("${file.path}")
private String path;
//文件路徑
String path = System.getProperty("java.class.path");
int firstIndex = path.lastIndexOf(System.getProperty("path.separator")) + 1;
int lastIndex = path.lastIndexOf(File.separator) + 1;
path = path.substring(firstIndex, lastIndex) + this.path+pathId;
path = URLDecoder.decode(path, "utf-8");
//在相應路徑下創建文件,若沒有此目錄或文件自動創建
File outputFile = new File(path+".png");
if (!outputFile.exists()) {
outputFile.createNewFile();
}
//文件寫入
FileOutputStream out = new FileOutputStream(filePath + fileName);
out.write(file);
out.flush();
out.close();





免責聲明!

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



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