2016-05-17 83 views
1

由Maven組裝後,將war-file放在外部Tomcat服務器上。當servlet啓動時,有一個FileNotFoundException,它找不到2.html文件(路徑相對於 - "2.html")。該文件位於應用程序的根目錄中。當我使用內置的Eclipse內部服務器Tomacat(Ran as - 在服務器上運行)啓動項目時 - 一切正常,文件已找到(路徑絕對是 - "F: /javaEEBelhard/Task-4/WebContent/WEB-INF/1.html")。Servlet/Tomcat文件路徑

這裏是Eclipse代碼:

try 
    { 
     FileInputStream fileIntputStream = new FileInputStream("2.html"); 
     InputStreamReader inputStreamReader = new InputStreamReader(fileIntputStream, CHARSET_NAME); 
     BufferedReader reader = new BufferedReader(inputStreamReader); 
     String line; 
     while ((line = reader.readLine()) != null) 
     { 
      writer.println(line); 
     } 
    } 
    catch (UnsupportedEncodingException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
+1

你是不是缺少一個左括號'{''後try'? – Smittey

+0

它不會編譯.. –

回答

0

如果部署的戰爭 - 有你的問題。我敢打賭,該文件不存在,因爲它在WAR文件中。 (文件API着眼於文件系統)

如果你需要閱讀的文件可靠 - 使用servletContext.getResource(path)servletContext.getResourceAsStream(path)