【问题标题】:Can't locate a file in WildFly REST application [duplicate]在 WildFly REST 应用程序中找不到文件 [重复]
【发布时间】:2018-10-30 22:58:33
【问题描述】:
@Path("/other")
public class Testclass {
   @GET
   @Path("/filepath")
   @Produces("text/html")
   public FileInputStream login() {
       File file = new File("standalone/deployments/domaci8.war/login.html");
       try {
           return new FileInputStream(file.getAbsolutePath());
       } catch (FileNotFoundException e) {
           e.printStackTrace();
           return null;
       }
   }
}

file.getAbsolutePath() 方法返回:

C:\Program Files (x86)\wildfly-10.1.0\bin\standalone\deployments\domaci8.war\login.html

而login.html文件位于这里:

C:\Program Files (x86)\wildfly-10.1.0\standalone\deployments\domaci8.war\login.html

【问题讨论】:

    标签: java rest path jax-rs wildfly


    【解决方案1】:

    File file = new File("standalone/deployments/domaci8.war/login.html"); 只是创建一个File 对象,文件的路径是相对于JVM 进程启动的文件夹。

    由于您已使用 standalone.batC:\Program Files (x86)\wildfly-10.1.0\bin 启动 WildFly 服务器,这就是 file.getAbsolutePath() 返回 C:\Program Files (x86)\wildfly-10.1.0\bin\standalone\deployments\domaci8.war\login.html 的原因

    如果login.html 与其他服务在同一个应用程序中,请检查https://stackoverflow.com/a/1768290/916225 这个答案。

    【讨论】:

    • file.getParent() 将在我的问题中返回null,所以我找到的唯一解决方案是创建一个文件new File("") 然后创建String path = file.getAbsolouthPath(),用path = path.substring(0, path.indexOf("\\bin")) 修剪字符串,然后你需要连接path += "\\standalone\\deployments\\domaci8.war\\login.html\\",它就是这样工作的。感谢您的帮助。
    • 这个帖子终于解决了我的问题stackoverflow.com/questions/23845031/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多