1.使用文件映射
application.yml
# 外部文件映射
web:
resources:
static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${demo.web.upload-path}
demo:
web:
upload-path: C:/test/
即直接将 C:/test/ 目录设置为静态资源路径 直接可通过【IP:[端口]】/xxx.txt 方式访问
2.获取当前路径的方法
Code
try {
ApplicationHome ah = new ApplicationHome(QqBotServeApplication.class);
File file = ah.getSource();
System.out.println(file.getParentFile().toString());
String path = (String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))).replaceAll("file:/", "").replaceAll("%20", " ").trim();
System.out.println(path); //jar包打包运行时的路径
String property = System.getProperty("user.dir");
System.out.println(property); //当前jar包的所在位置(可以用这个直接将文件写入外部)
String savePathName = property + "/test111.jpg";
File file1 = new File("test222.txt");
file1.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}