java.io.EOFEException:由于输入结束,没有要映射到对象的内容
我试图在服务器(tomcat)部署时加载文件。如果我从eclipse运行webapp,它将非常有效。 但如果我发动战争并部署它,我会得到这个例外java.io.EOFEException:由于输入结束,没有要映射到对象的内容,java,json,tomcat7,classloader,filereader,Java,Json,Tomcat7,Classloader,Filereader,我试图在服务器(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.ma
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()代码>
及
但我还是犯了同样的错误。请帮帮我。
这是我的VEMSApplicationObject.java代码,其中fileName=“data/budgetSetup.json”
以下是异常java.io.FileNotFoundException:C:\Program%20Files\Apache%20Software%20Foundation\Tomcat%207.0\webapps\VEMS\WEB-INF\classes\data\budgetSetup.json(系统找不到指定的路径)如果要在Tomcat启动时加载某些资源,可以使用以下方法:
1-在web.xml文件中添加:
<listener>
<listener-class>com.mycompany.mywebapp.ContextListener</listener-class>
</listener>
3-将json文件放在/WEB-INF/data下/
现在,tomcat应该在启动时加载您的文件。如果您想在tomcat启动时加载一些资源,可以使用以下方法:
1-在web.xml文件中添加:
<listener>
<listener-class>com.mycompany.mywebapp.ContextListener</listener-class>
</listener>
3-将json文件放在/WEB-INF/data下/
现在,tomcat应该在启动时加载您的文件。解决了这个问题。tomcat安装路径是问题所在(C:\ProgramFiles\Apache Software Foundation\tomcat 7.0
)。因为它包含空间,即程序和文件之间的空间和Apache、软件和基金会之间的空间。因此,在上载war时,此路径将转换为C:\Program%20Files\Apache%20Software%20Foundation\Tomcat%207.0
,并导致java.io.FileNotFoundException:
因此,在c:\tomcat
上安装tomcat解决了这个问题。tomcat安装路径是问题所在(C:\ProgramFiles\Apache Software Foundation\tomcat 7.0
)。因为它包含空间,即程序和文件之间的空间和Apache、软件和基金会之间的空间。因此,在上载war时,此路径将转换为C:\Program%20Files\Apache%20Software%20Foundation\Tomcat%207.0
,并导致java.io.FileNotFoundException:
因此,请在c:\tomcat
安装tomcat,感谢您提供的解决方案。它工作得很好。请告诉我为什么ClassLoader=this.getClass().getClassLoader();file=新文件(classLoader.getResource(fileName.getFile())代码>不工作。这是工作,因为过去几个月现在突然停止工作。感谢您提供的解决方案。它工作得很好。请告诉我为什么ClassLoader=this.getClass().getClassLoader();file=新文件(classLoader.getResource(fileName.getFile())代码>不工作。这是工作,因为过去几个月现在突然停止工作。
`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 "";
}`
<listener>
<listener-class>com.mycompany.mywebapp.ContextListener</listener-class>
</listener>
package com.mycompany.mywebapp;
import java.io.File;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ContextListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent event) {
}
@Override
public void contextInitialized(ServletContextEvent event) {
try {
String webroot = event.getServletContext().getRealPath("/");
File myFile = new File(webroot + "/WEB-INF/data/budgetSetup.json");
/***
* do your work with file etc...
*/
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
}