GAE上的Java聊天应用程序,无法将消息写入文件
我在谷歌应用引擎平台上的JSF程序有问题。当我读到GAE不支持FileOutputStream类时,我几乎完成了在JavaEE中实现聊天应用程序。 通过这个类对象,我创建了一个文件,在里面写聊天信息,通过scirpt,这个文件被加载并刷新到index.xhtml网站上 我需要帮助,因为我不知道我可以替换哪个类来完成这个应用程序。我发现,所以我知道这是可能的,但如何在Java中实现它呢 如果有任何帮助,我将不胜感激 下面我粘贴了一段带有FileOutputSream操作的ChatBean类:GAE上的Java聊天应用程序,无法将消息写入文件,java,google-app-engine,jsf,java-ee-6,Java,Google App Engine,Jsf,Java Ee 6,我在谷歌应用引擎平台上的JSF程序有问题。当我读到GAE不支持FileOutputStream类时,我几乎完成了在JavaEE中实现聊天应用程序。 通过这个类对象,我创建了一个文件,在里面写聊天信息,通过scirpt,这个文件被加载并刷新到index.xhtml网站上 我需要帮助,因为我不知道我可以替换哪个类来完成这个应用程序。我发现,所以我知道这是可能的,但如何在Java中实现它呢 如果有任何帮助,我将不胜感激 下面我粘贴了一段带有FileOutputSream操作的ChatBean类: @S
@Stateful
@ApplicationScoped
@ManagedBean(name="Chat")
public class ChatBean {
private List<String> users = new ArrayList<String>();
private String newUser;
FileOutputStream chatHtmlBufferWriter;
public ChatBean () throws IOException {
ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String chatHtmlPath = ctx.getRealPath("/") + "chat";
try {
this.chatHtmlBufferWriter = new FileOutputStream(chatHtmlPath);
this.chatHtmlBufferWriter.write("Start chatu ąęć. <br />".getBytes("UTF-8"));
} catch (IOException ex) {
this.chatHtmlBufferWriter.close();
throw ex;
}
users.add("Admin");
}
@PreDestroy
public void closeFileBuffor() throws Exception {
this.chatHtmlBufferWriter.close();
}
public String addMessage(String msg) throws IOException {
this.chatHtmlBufferWriter.write(msg.getBytes("UTF-8"));
FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
return "index";
}
...
}
@Stateful
@适用范围
@ManagedBean(name=“Chat”)
公共类聊天豆{
private List users=new ArrayList();
私有字符串newUser;
FileOutputStream chatHtmlBufferWriter;
公共ChatBean()抛出IOException{
ServletContext ctx=(ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
字符串chattmlPath=ctx.getRealPath(“/”+“chat”;
试一试{
this.chattmlBufferWriter=新文件输出流(chattmlPath);
this.chattmlBufferWriter.write(“Start chatuąęć.
”.getBytes(“UTF-8”);
}捕获(IOEX异常){
this.chattmlBufferWriter.close();
掷骰子;
}
用户。添加(“管理员”);
}
@发情前期
public void closeFileBuffer()引发异常{
this.chattmlBufferWriter.close();
}
公共字符串addMessage(字符串消息)引发IOException{
this.chattmlBufferWriter.write(msg.getBytes(“UTF-8”);
FacesContext.getCurrentInstance().getExternalContext().redirect(“index.xhtml”);
返回“索引”;
}
...
}
和index.xhtml文件中的脚本:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
var currPos = 0;
var loadChat = function() {
$("#chatForm\\:chatbox").load('chat');
currPos = $(".chat")[0].scrollHeight;
$(".chat").scrollTop(currPos);
}
var scrollChat = function() {
$("#chatForm\\:chatbox").load('chat');
$(".chat").scrollTop(currPos);
}
var currPos;
$(document).ready(function() {
$("#chatForm\\:chatbox").load('chat', function(){
loadChat();
});
var refreshId = setInterval(function() {
scrollChat();
}, 1000);
$.ajaxSetup({ cache: false });
$("#chatForm\\:chatbox").scroll(function() {
currPos = $(".chat").scrollTop();
});
});
</script>
var currPos=0;
var loadChat=函数(){
$(“#chatForm\\:chatbox”).load('chat');
currPos=$(“.chat”)[0]。滚动高度;
$(“.chat”).scrollTop(currPos);
}
var scrollChat=函数(){
$(“#chatForm\\:chatbox”).load('chat');
$(“.chat”).scrollTop(currPos);
}
var-currPos;
$(文档).ready(函数(){
$(“#chatForm\\:chatbox”).load('chat',function(){
loadChat();
});
var refreshId=setInterval(函数(){
scrollChat();
}, 1000);
$.ajaxSetup({cache:false});
$(“#chatForm\\:chatbox”)。滚动(函数(){
currPos=$(“.chat”).scrollTop();
});
});
基本上,您不能直接写入文件系统(尽管您可以读取)
您将需要使用一个现有的GAE存储API,例如具有。有关其他选项的详细信息,请参见
然而,我不确定你是否正确思考了这个问题;您只需要创建一个GET方法,该方法返回当前消息并由脚本调用。这些消息将永远不会写入文件。首先,您可以将消息存储在内存中。我怀疑你链接到的教程也会这样做
(更新:我最初说FileOutputStream在白名单中,但我正在查看
FilterOutputStream
.Oops。)谢谢你的回答,我会检查一下。现在我开始思考解决这个问题的另一种方法。也许可以在不使用文件的情况下将消息写入对象并显示它?是的,这基本上就是我的建议。为“/messages”URL或应返回当前消息列表的类似URL实现GET方法。然后从脚本中对这个URL进行AJAX调用,它应该将消息插入到页面中。我不太确定它应该是什么样子。您可以发布这个解决方案的源代码的示例结构吗?为什么不看看Python示例并做一些类似的事情呢?