2017-08-04 26 views
1

我試圖在服務器(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(該系統找不到指定的路徑)

回答

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 /數據/

現在tomcat的應在啓動時加載文件。

+0

謝謝你提供的解決方案。它運作良好。請告訴我爲什麼 'ClassLoader classLoader = this.getClass()。getClassLoader(); file = new File(classLoader.getResource(fileName).getFile());' 不起作用。自從過去幾個月以來,這個工作突然停止了。 –

0

解決了這個問題。 tomcat安裝路徑是問題(C:\Program Files\Apache Software Foundation\Tomcat 7.0)。由於它包含空間,即程序&文件和Apache之間的空間,軟件&基金會之間的空間。因此,在上傳戰爭時,此路徑轉換爲C:\Program%20Files\Apache%20Software%20Foundation\Tomcat%207.0並導致java.io.FileNotFoundException:

所以安裝tomcat c:\tomcat

相關問題