2017-03-27 82 views
1

你好,我正在使用program abjava創建一個聊天bot web應用程序。如果我從外部存儲訪問文件,它正在工作。如果我將文件存儲在我的項目中,它不起作用?我不知道是什麼問題。在哪裏包括aiml bot文件

此代碼無效,ServerCode是我在eclipse中的項目名稱。

String botName = "super"; 
String botPath = "ServerCode/aiml"; 
Bot bot = new Bot(botName, botPath); 
Chat chatSession = new Chat(bot); 
String request = msg.toUpperCase(); 
String response = chatSession.multisentenceRespond(request);  
System.out.println(response); 

雖然這個代碼工作,

String botName = "super"; 
String botPath = "E:\ab"; 
Bot bot = new Bot(botName, botPath); 
Chat chatSession = new Chat(bot); 
String request = msg.toUpperCase(); 
String response = chatSession.multisentenceRespond(request);  
System.out.println(response); 

問題是什麼?如何將文件夾及其子文件夾存儲在項目中,以便當我可以創建war文件時,我將這些文件包含在該文件的war文件中。

回答

0

存儲文件的位置沒有問題,問題在於您如何訪問它。當您將文件導出到war文件時,文件會存儲在其他某個位置,因此您無法訪問這些文件。所以通過改變你訪問文件的方式如下,它會正常工作。

String botPath = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); 
String botName = "super"; 
String botPath = "E:\ab"; 
Bot bot = new Bot(botName, botPath); 
Chat chatSession = new Chat(bot); 
String request = msg.toUpperCase(); 
String response = chatSession.multisentenceRespond(request);  
System.out.println(response);