【发布时间】:2017-08-04 13:03:45
【问题描述】:
我正在尝试在服务器(tomcat)部署时加载文件。如果我从 Eclipse 运行 webapp,它会很好用。 但是如果我发动战争并部署它,我会得到这个异常
java.io.EOFException: No content to map to Object due to end of input
at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2173)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2125)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1395)
at com.vems.util.JSONUtil.jsonToObject(JSONUtil.java:76)
at com.vems.util.DataUtil.loadData(DataUtil.java:54)
at com.vems.security.VEMSApplicationObject.loadApplicationSetups(VEMSApplicationObject.java:65)
at com.vems.security.VEMSApplicationObject.startApplication(VEMSApplicationObject.java:53)
at com.vems.security.VEMSContextListener.contextInitialized(VEMSContextListener.java:16)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5099)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5615)
我试过ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
和
`<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
`
和
`InputStream in=ClassLoader.getSystemResourceAsStream("data/budgetSetup.json");
BufferedReader br = new BufferedReader(new InputStreamReader(in));`
但我仍然遇到同样的错误。请帮我。 这是我的 VEMSApplicationObject.java 代码,其中 fileName = "data/budgetSetup.json";
`public String readFile(String fileName, boolean resourceFile) {
try {
File file;
if (resourceFile) {
ClassLoader classLoader = this.getClass().getClassLoader();
file = new File(classLoader.getResource(fileName).getFile());
} else {
file = new File(fileName);
}
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
return sb.toString();
} finally {
fr.close();
br.close();
}
} catch (Exception e) {
System.out.println("Here is The Exception " + e);
}
return "";
}`
这里是异常 java.io.FileNotFoundException: C:\Program%20Files\Apache%20Software%20Foundation\Tomcat%207.0\webapps\VEMS\WEB-INF\classes\data\budgetSetup.json(系统找不到指定的路径)
【问题讨论】:
标签: java json tomcat7 classloader filereader