首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在WEB-INF位置加载文件

无法在WEB-INF位置加载文件
EN

Stack Overflow用户
提问于 2017-09-16 00:57:57
回答 1查看 269关注 0票数 0

在我的war中的以下位置有一个json文件

代码语言:javascript
复制
   WEB-INF/classes/META-INF/jsonFolder/file.json

我正在尝试使用路径访问文件,并使用以下方法加载我的文件。

代码语言:javascript
复制
public static File deserializeJSONFile() throws IOException {
    File file = null;
    OutputStream outStream = null;
String jsonPath = "jsonFolder/file.json";
try (InputStream inputStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream(jsonPath)) {
        in = inputStream;
        if (in != null) {
            byte[] bufferArray = new byte[in.available()];
            in.read(bufferArray);
            file = new File("tempJsonFile.json");
            outStream = new FileOutputStream(file);
            outStream.write(bufferArray);
        }
    } finally {
        if (outStream != null) {
            outStream.close();
        }
    }
    if (null == in) {
        String errorMessage = "JSON file: " + jsonPath + " could not be loaded";
        FileNotFoundException fileNotFoundException = new FileNotFoundException(errorMessage);
        logger.error(errorMessage, fileNotFoundException);
        throw fileNotFoundException;
    }
    return file ;
  }

我所有的单元测试都通过了。在部署我的应用程序时,我看到jsonFolder/file.json的文件未找到异常。当我追踪到它正在搜索的实际位置时,它看着,

代码语言:javascript
复制
 C:\Program Files\eclipse-mars\jsonFolder\file.json

这是不正确的。我尝试了不同的类加载器方法,但没有帮助。一条规则是我不应该修改pom.xml来使其工作。如何加载文件?

EN

回答 1

Stack Overflow用户

发布于 2017-09-16 02:21:53

在我的war中的以下位置有一个json文件

WEB-INF->classes->META-INF->jsonFolder->file.json

因此,它作为资源的名称是META-INF/jsonFolder/file.json。而不是您的代码中的内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46244413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档