简体   繁体   English

GAE上的Java聊天应用程序,无法将消息写入文件

[英]Java chat application on GAE, trouble with write messages to file

I have problem with my program in JSF on Google App Engine platform. 我在Google App Engine平台上的JSF中的程序遇到问题。 I almost finished implement chat application in Java EE, when I read that class FileOutputStream isn't supported by GAE. 当我读到GAE不支持FileOutputStream类时,我几乎用Java EE实现了聊天应用程序。 By this class object I create file, to write inside it chat messages and by scirpt this file is loaded and refreshed on index.xhtml website. 通过该类对象,我创建文件,以在其中写入聊天消息,并通过脚本将该文件加载并在index.xhtml网站上刷新。

I need help because I don't know which class can I replace FileOutputStream to finish this application. 我需要帮助,因为我不知道可以替换哪个类的FileOutputStream来完成此应用程序。 I found example in Python so I know that this is possible, but how to implement it in Java? 在Python中找到了示例,所以我知道这是可能的,但是如何在Java中实现呢?

I will be grateful for any help. 我将不胜感激。

Below I paste piece of class ChatBean with FileOutputSream operations: 下面,我使用FileOutputSream操作粘贴了ChatBean类:

@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"; 
    } 
...
}

And script inside index.xhtml file: 以及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>

Basically, you can't directly write to the File System (although you can read). 基本上,您不能直接写入文件系统(尽管可以读取)。

You will need to use one of the existing GAE storage APIs, such as the blobstore which has a File like API . 您将需要使用现有的GAE存储API之一,例如具有File like API的blobstore。 Other options are detailed on the Storing Data page . 其他选项在“ 存储数据”页面上有详细说明。

However, I'm not sure you're thinking about this correctly; 但是,我不确定您是否在考虑正确; you just want to create a GET method that returns the current messages and is called by your script. 您只想创建一个返回当前消息并由您的脚本调用的GET方法。 The messages will never be written to file at all. 消息将永远不会被写入文件。 To begin with, you could just store the messages in memory. 首先,您可以只将消息存储在内存中。 I suspect the tutorial you link to does the same. 我怀疑您链接到的教程也是如此。

(UPDATE: I originally said FileOutputStream was in the whitelist, but I was looking at FilterOutputStream . Oops.) (更新:我最初说FileOutputStream在白名单中,但是我在看FilterOutputStream 。糟糕。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM