2016-08-05 51 views
0

當我嘗試讀取我彈簧控制器中的屬性文件時,我收到FileNotFoundExceptionSpring MVC中的屬性文件的FileNotFoundError

這裏是顯示在何時何地出現日誌:

java.io.FileNotFoundException: src\main\webapp\WEB-INF\props\configFile.properties (The system cannot find the file specified) 
at java.io.FileInputStream.open0(Native Method) 
at java.io.FileInputStream.open(FileInputStream.java:195) 
at java.io.FileInputStream.<init>(FileInputStream.java:138) 
at com.webclaims.translator.Translator.readPropertiesFile(Translator.java:65) 
at com.webclaims.translator.Translator.createEditablePage(Translator.java:45) 
at com.webclaims.translator.controllers.TestController.editableWebpage(TestController.java:34) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 

我的屬性文件的位置是src/main/webapp/WEB-INF/props/configFile.properties

這裏是有一個在日誌中顯示editableWebpage()控制器:

@RequestMapping(value = "/edit") 
public ModelAndView editableWebpage() throws IOException { 
    final String source = "http://localhost:8080/translator/test"; 
    final String target = "src/main/webapp/WEB-INF/jsp/editable_webpage.jsp"; 
    final String config = "src/main/webapp/WEB-INF/props/configFile.properties"; 

    Translator t = new Translator(); 
    t.createEditablePage(source, target, config); 

    return new ModelAndView("editable_webpage"); 
} 

然後我們去我的Java類,它有一個以上的日誌中看到的createEditablePage()

@PropertySource(value = "configFile.properties") 
public class Translator { 

@Autowired 
private Properties properties; 

private static final Logger LOGGER = Logger.getLogger(Translator.class.getName()); 

public Translator() { 
    this.properties = new Properties(); 
} 

public Properties getProperties() { 
    return properties; 
} 

public void setProperties(Properties properties) { 
    this.properties = properties; 
} 

public void createEditablePage(String source, String target, String config) { 
    File file = new File(target); 
    try { 
     Document doc = Jsoup.connect(source).get(); 
     Elements elements = doc.select("*"); 
     readPropertiesFile(config); 
     for(Element element : elements) { 
      if(!element.ownText().equals("")) { 

       String key = getKeyFromPropertiesFile(element.text().toString()); 
       if(!key.equals("")) { 
        element.addClass(key); 
        element.attr("contentEditable", "true"); 
       } 
      } 
     } 
     FileUtils.writeStringToFile(file, doc.outerHtml(), "UTF-8"); 
    } catch (IOException e) { 
     LOGGER.log(Level.SEVERE, "IOException has occured", e); 
    } 
} 

private void readPropertiesFile(String config) { 
    try { 
     File propsFile = new File(config); 
     FileInputStream inputStream = new FileInputStream(propsFile); 
     properties.load(inputStream); 
     inputStream.close(); 
    } catch(FileNotFoundException e) { 
     LOGGER.log(Level.SEVERE, "FileNotFoundException has occured", e); 
    } catch(IOException e) { 
     LOGGER.log(Level.SEVERE, "IOException has occured", e);  
    } 
} 

以下是我在我的web.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> 
    <display-name>translator</display-name> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring-servlet.xml</param-value> 
    </context-param> 
    <servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

我的彈簧servlet.xml中含有兩個豆,其中一人被講述的屬性文件春天。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:p="http://www.springframework.org/schema/p"  
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:context="http://www.springframework.org/schema/context"  
xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 

<mvc:annotation-driven/> 
<context:component-scan base-package="com.webclaims.*" /> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/jsp/"></property> 
    <property name="suffix" value=".jsp"></property> 
</bean> 

<bean id="propertiesFile" class="com.webclaims.translator.Translator"> 
    <property name="properties" value="/WEB-INF/props/configFile.properties"></property> 
</bean> 

+0

你有沒有試圖把'configFile.properties'資源文件夾? –

+0

我該如何訪問它?是不是像'classpath:configFile.properties'? – cod3min3

+0

是的。 classpath:configFile.properties – kuhajeyan

回答

0

你的位置不匹配!

在屬性值指定值= 「/ WEB-INF /道具/ configFile.properties」

,但你提到你的文件是在 「/WEB-INF/config/configFile.properties」

如果你仍然面臨的問題儘量把屬性文件在src文件夾

+0

這是一個錯字。現在已修好。 – cod3min3

0

試試這個..

值= 「的src /主/ web應用/ WEB-INF /道具/ configFile.properties」

0

Oups,這裏有一些關鍵概念的誤解...

我的屬性文件的位置是src/main/webapp/WEB-INF/props/configFile.properties。不。這是您的財產文件來源的位置。你有一個構建過程會建立一個.war文件,並且這個war文件將由一個servlet容器運行。在那一刻(運行時間),所有對源文件的引用都已丟失。

構建過程將(從廣義上說,並假設一個Maven構建):

  • 編譯所有的java文件類文件,將它們放在/ WEB-INF/classes中
  • 副本從SRC的所有文件/在/ WEB-INF/classes中主/資源,他們將在運行時直接下的所有文件從的src/main/webapp的資源與ClassLoader.getResourceClassLoader.getResourceAsStream
  • 副本訪問/並會在運行時與ServletContext.getResource資源訪問或ServletContext.getResourceAsStream

將資源用於只讀數據會更好,因爲您可以透明地使用位於應用程序內部部署的jar文件夾的META-INF/resources文件夾中的資源。

無論如何,如果你真的需要打開Web應用程序的文件,唯一正確的方法是使用的方法ServletContext.getRealPath相對於應用程序的根目錄的路徑轉換成絕對路徑服務器上(並沒有在developper的源更長)

所以讀取屬性文件,你應該得到它從ServletContex一個的ressource:

  • 在您控制器使用相對路徑,並獲得InputStream

    @RequestMapping(value = "/edit") 
    public ModelAndView editableWebpage(ServletRequest request) throws IOException { 
        final String source = "http://localhost:8080/translator/test"; 
        final String config = "/WEB-INF/props/configFile.properties"; 
        ... 
        InputStream inputStreamConfig = request.getServletContext() 
         .getResourceAsStream(config); 
        t.createEditablePage(source, target, inputStreamConfig); 
    
        return new ModelAndView("editable_webpage"); 
    } 
    
  • readPropertiesFile方法

    ,只需加載性能:

    properties.load(inputStreamConfig); 
    

但也有其他的問題是你的代碼:

  • Translator類,你有兩個:

    @Autowired 
    private Properties properties; 
    

    意味着性能應注射,並

    public Translator() { 
        this.properties = new Properties(); 
    } 
    

    在構造函數初始化。 一個是無用的

  • Translator類使用Autowired註釋,所以它應該是一個Spring bean,但是您可以使用new創建它。不這樣做,但是把它注射在控制器

  • 在web.xml中,你申報的DispatcherServlet中=>每個bean將被創建兩次,一次在每個上下文根上下文相同的文件。但是你沒有聲明任何ContextLoaderListener來引導Spring。這是常見的發現這一點:

    <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener>