如何获取位于JAR文件之外的文件的URL?

huangapple 未分类评论74阅读模式
英文:

How can I get the Url of file that is outside the jar?

问题

我已经使用maven-assembly-plugin在爆炸模式下部署了Spring Boot应用程序。我已经从项目的资源文件夹创建了config文件夹。

问题是,我无法获取访问config文件夹中文件的URL。

如何获取我已上传并存储在此config文件夹中文件的URL。

文件结构

目标文件夹:

+springdemo-0.0.1-application.zip
   +config
     +myfolder
        -oldfile.png
   +springdemo-0.0.1.jar	//运行此jar文件
   +start.sh
-springdemo-0.0.1.jar	

我想要获取存储在myfolder中的文件的URL。这个URL将由第三方(例如Picasso)访问以获取文件数据。我无法获得指向这些myfolder文件的正确URL。

英文:

I have deployed spring boot app in exploded mode using maven-assembly-plugin. I have made config folder from project’s resource folder.

The problem is that I am not able to get the url to access the file that is in the config folder.

How can I get the url of the file that I have uploaded and stored at this config folder.

File structure

Target folder:

+springdemo-0.0.1-application.zip
   +config
     +myfolder
        -oldfile.png
   +springdemo-0.0.1.jar	//running this jar file
   +start.sh
-springdemo-0.0.1.jar	

I want URL of files stored in myfolder. This url will be accessed by 3rd party(for ex. Picasso) to get file data. I am unable to get correct url pointing to this myfolder files.

答案1

得分: 0

Note: 不是SpringBoot特定的答案,而是简单的Java方法。

由于在运行Java应用程序时无法知道工作目录,因此您只能尝试猜测文件的位置。

例如,如果您控制Java应用程序的执行方式,可以尝试非常简单的方法:

var configFile = new File("config/application.properties");

只要进程是从与start.sh相同的目录启动的(这似乎是您的意图),这将起作用。

如果不起作用,首先尝试像这样打印工作目录:

System.out.println("WRK DIR = " + new File(".").getAbsolutePath());

这将告诉您文件的相对路径应该是什么。
因此,如果这个打印输出是 <root-dir>/springdemo,并且您知道配置文件在 <root-dir>/springdemo/mydir/config/ 下,那么文件路径应该是:

new File("mydir/config/application.properties");

顺便说一下,您可以轻松地使用以下代码读取文件:

// 将文件读取为行的列表
Files.readAllLines(configFile);

// 特别是对于属性文件
var props = new Properties();
props.load(new FileInputStream(configFile));

如果出于某种原因,您需要一个URL对象而不是File对象,很简单,只需调用:

URL url = configFile.toURI().toURL();

如果您想获取配置目录下的所有文件,可以首先列出它们:

File[] files = configFile.getParentFile().listFiles();
if (files != null) {
    for (File file : files) {
        // 使用文件?
    }
}
英文:

Note: Not a SpringBoot-specific answer, just the simple Java way.

You can only try to guess the location of the file because there's no way to know the working directory when a Java application is run.

For example, you could try a very simple approach if you control how the Java application is executed:

var configFile = new File(&quot;config/application.properties&quot;);

This will works as long as the process is started from the same directory where start.sh is (which seems like what you intend).

If it doesn't work, try printing the working directory like this first:

System.out.println(&quot;WRK DIR = &quot; + new File(&quot;.&quot;).getAbsolutePath());

This will tell you what the relative path to your file should be.
So, if this prints &lt;root-dir&gt;/springdemo and you know your config file is under &lt;root-dir&gt;/springdemo/mydir/config/, then the file path should be:

new File(&quot;mydir/config/application.properties&quot;);

By the way, you can easily read the file with:

// read file into List of lines
Files.readAllLines(configFile);

// specifically, for properties file
var props = new Properties();
props.load(new FileInputStream(configFile));

If for whatever reason you need an URL object instead of a File, it's easy, just call:

URL url = configFile.toURI().toURL()

If you want ALL files under the config dir, you can list them first with:

File[] files = configFile.getParentFile().listFiles();
if (files != null) {
    for (File file : files) {
        // use file?
    }
}

答案2

得分: 0

很简单,您需要在运行spring-boot.jar应用程序时,提供外部application.properties文件的完整路径,该路径与源代码中的路径相同。

示例:java -jar -Dspring.config.location=file://<<带有文件名的完整文件路径>>

这个外部文件优先于我们的jar文件中的文件。

英文:

Its easy, you need to provide the full path of the external application.properties file than what is present in your source code during the startup of the spring-boot.jar application at runtime.

Example: java -jar -Dspring.config.location=file://<<full file path with name>>

This external file takes precedence over the one in our jar file.

huangapple
  • 本文由 发表于 2020年8月15日 03:21:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/63418907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定