我試圖在服務器(tomcat)部署時加載文件。如果我從eclipse運行webapp,它會很好用。 但是,如果我做的戰爭和部署它,我得到這個異常java.io.EOFException:由於輸入結束而沒有內容映射到對象
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:\程序%20Files \阿帕奇%20Software%20Foundation \ Tomcat的%207.0 \ web應用\ VEMS \ WEB-INF \類\數據\ budgetSetup.json(該系統找不到指定的路徑)
謝謝你提供的解決方案。它運作良好。請告訴我爲什麼 'ClassLoader classLoader = this.getClass()。getClassLoader(); file = new File(classLoader.getResource(fileName).getFile());' 不起作用。自從過去幾個月以來,這個工作突然停止了。 –