java.io.EOFException: No content to map to Object due to end of input

时间:2017-08-04 13:03:45

标签: java json tomcat7 classloader filereader

I am trying to load file at server(tomcat) deployment time. It is working great in case if i run webapp from eclipse. But if i make war and deploy it i get this exception

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)

I tried ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

and

`<build>
<resources>
    <resource>
        <filtering>true</filtering>
        <directory>src/main/resources</directory>
    </resource>
</resources>

`

and

`InputStream in=ClassLoader.getSystemResourceAsStream("data/budgetSetup.json");
BufferedReader br = new BufferedReader(new InputStreamReader(in));`

But still i am getting same error. Please help me. Here is my VEMSApplicationObject.java code where 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 "";
    }`

Here is The Exception java.io.FileNotFoundException: C:\Program%20Files\Apache%20Software%20Foundation\Tomcat%207.0\webapps\VEMS\WEB-INF\classes\data\budgetSetup.json (The system cannot find the path specified)

2 个答案:

答案 0 :(得分:0)

如果要在tomcat启动时加载某些资源,可以使用以下方法:

1 - 在你的web.xml文件中添加:

<listener>
    <listener-class>com.mycompany.mywebapp.ContextListener</listener-class>
</listener>

2 - 在您的项目中创建包com.mycompany.mywebapp和类ContextListener

    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();
            }
        }
    }

3 - 将您的json文件放在/ WEB-INF / data /

现在tomcat应该在启动时加载你的文件。

答案 1 :(得分:0)

解决了这个问题。 tomcat安装路径是问题(C:\Program Files\Apache Software Foundation\Tomcat 7.0)。因为它包含空格,即Program&amp; amp;和Apache,Software&amp; amp;之间的文件和空间基础。因此,在上传war时,此路径会转换为C:\Program%20Files\Apache%20Software%20Foundation\Tomcat%207.0并转到java.io.FileNotFoundException:

所以在c:\tomcat

安装tomcat