问题描述
我已经使用maven-assembly-plugin以分解模式部署了spring-boot应用程序。目标文件夹包含以下结构:
+springdemo-0.0.1-application.zip
+config
+myfolder
-profile.png
+springdemo-0.0.1.jar //running this jar file
+start.sh
-springdemo-0.0.1.jar
我想要控制器类中的profile.png文件的网址。
我已经尝试过了,但是它给了我网址:http:// ip:port / config / myfoler / profile.png。
getimageDownloadUri("config/myfolder/" + filename)
public static String getimageDownloadUri(String imagePath) {
return ServletUriComponentsBuilder.fromCurrentcontextpath().path(imagePath).toUriString();
}
我也尝试过这样做,这给了我url:null
MainController.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
解决方法
您无法从springdemo-0.0.1.jar
内部可靠地获取路径,因为它取决于从何处启动JVM。也就是说,您可以执行以下操作:
File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getPath();
用真实的类名替换MyClass
。
另一种选择是设置指向部署目录的环境变量,然后在MyClass
中使用它。