在我的war中的以下位置有一个json文件
WEB-INF/classes/META-INF/jsonFolder/file.json我正在尝试使用路径访问文件,并使用以下方法加载我的文件。
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的文件未找到异常。当我追踪到它正在搜索的实际位置时,它看着,
C:\Program Files\eclipse-mars\jsonFolder\file.json这是不正确的。我尝试了不同的类加载器方法,但没有帮助。一条规则是我不应该修改pom.xml来使其工作。如何加载文件?
发布于 2017-09-16 02:21:53
在我的war中的以下位置有一个json文件
WEB-INF->classes->META-INF->jsonFolder->file.json
因此,它作为资源的名称是META-INF/jsonFolder/file.json。而不是您的代码中的内容。
https://stackoverflow.com/questions/46244413
复制相似问题