【问题标题】:Google App Engine - Data being stored in a weird wayGoogle App Engine - 数据以一种奇怪的方式存储
【发布时间】:2011-08-08 02:44:24
【问题描述】:

我正在使用 Java。这是插入数据存储区的纯数据:

<p>Something</p>\n<p>That</p>\n<p> </p>\n<p>Should.</p>\n<p> </p>\n
<p>I have an interesting question.</p>\n<p>Why are you like this?</p>\n
<p> </p>\n<p>Aren't you fine?</p>

这是它的存储方式:

<p>Something</p> <p>That</p> <p>�</p> <p>Should.</p> <p>�</p> 
<p>I have an interesting question.</p> <p>Why are you like this?</p> 
<p>�</p> <p>Aren't you fine?</p>

奇怪的符号是怎么回事?这只发生在现场,而不是在我的本地 dev_appserver 上。

编辑

这是插入数据的代码:

String content = ""; // this is where the data is stored
try {
    ServletFileUpload upload = new ServletFileUpload();
    FileItemIterator iter = upload.getItemIterator(request);
    while(iter.hasNext()) {
        FileItemStream item = iter.next();
        InputStream stream = item.openStream();

        if(item.isFormField()) {
            String fieldName = item.getFieldName();
            String fieldValue = new String(IOUtils.toByteArray(stream), "utf-8");
            LOG.info("Got a form field: " +fieldName+" with value: "+fieldValue);
            // assigning the value
            if(fieldName.equals("content")) content = fieldValue;
        } else {
            ...
        }
    }
} catch (FileUploadException e){
}

...
// insert it in datastore
Recipe recipe = new Recipe(user.getKey(), title, new Text(content), new Text(ingredients), tagsAsStrings);
pm.makePersistent(recipe);

这是一个multipart/form-data 表单,所以我必须使用item.isFormField() 的小魔法来获取实际内容并构造一个字符串。也许这导致了奇怪的编码问题?不确定。

要检索数据,我只需这样做:

<%=recipe.getContent().getValue()%>

由于content 是文本类型(应用程序引擎类型),我使用.getValue() 来获得实际结果。我认为检索数据不是问题,因为我可以直接在在线应用引擎数据存储查看器中看到奇怪的字符。

【问题讨论】:

  • \n 是新行的指示符。你指的是这个吗?
  • 这是 99% 的编码问题;您如何处理代码中的编码?你能发布一些相关的sn-p吗?
  • @systempuntoout:我不处理它。我有什么特别的需要去做吗?
  • 你怎么知道它是这样存储的?你在哪里读这个?数据存储查看器还是 Jsp?
  • @systempuntoout:我在此处显示的内容直接来自数据存储查看器。 JSP 只显示一个普通的问号符号。

标签: java google-app-engine jdo


【解决方案1】:

你在使用 eclipse 吗?如果是,请在“文件”>“属性”>“文本文件编码”下检查您的文件是否为 UTF-8 编码。

我猜不会。

因此,将其更改为 UTF-8,您的问题应该会得到解决。

问候

迪迪尔

【讨论】:

  • 这只会处理 .java 源代码编码,这是一个单独的问题。
【解决方案2】:

按照此页面创建一个 Servlet 过滤器,以便我的所有页面都以 utf8 编码:

How to get UTF-8 working in Java webapps?

创建过滤器后,一切正常!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 2011-04-15
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多