如何在服务器上部署 WAR 文件时查找文件夹内的文件?

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

How to find files inside a folder when war file is deployed on server?

问题

我可以使用**getResourceAsStream()**函数来读取特定的文件。我已经尝试过使用相同的输入流来获取文件名,但那并没有起作用。以下是我用于此的代码:

BufferedReader rdr = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(path)));

String line;

while ((line = rdr.readLine()) != null) {
  LOG.info("测试文件:" + line);
}
rdr.close();

更多信息: ZIP 文件部署在 Wildfly 服务器上。

尝试使用以下代码来获取整个路径,但它会提供错误的位置:

this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()
英文:

I am able to read specific files using getResourceAsStream() function. I have tried using the same input stream for fetching file names but that did not work. This is the code that i used for that

BufferedReader rdr = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(path)));

String line;

while ((line = rdr.readLine()) != null) {
  LOG.info("Testing File: " + line);
}
rdr.close();

More info : zip file is deployed on wildfly server

Tried fetching whole path with this but it gives wrong location
this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()

huangapple
  • 本文由 发表于 2020年5月5日 15:39:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/61608072.html
匿名

发表评论

匿名网友

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

确定