简体   繁体   English

Google App Engine - 以奇怪的方式存储数据

[英]Google App Engine - Data being stored in a weird way

I'm using Java. 我正在使用Java。 This is the pure data that gets inserted in the datastore: 这是插入数据存储区的纯数据:

<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>

This is how it gets stored: 这是它的存储方式:

<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>

What's up with the weird symbols? 什么与奇怪的符号? This happens only live, not on my local dev_appserver. 这只发生在现场,而不是在我的本地dev_appserver上。

EDIT 编辑

Here's the code that inserts the data: 这是插入数据的代码:

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);

It's a multipart/form-data form so I have to do that little item.isFormField() magic to get the actual content, and construct a String. 它是一个multipart/form-data表单,所以我必须做一点item.isFormField()魔术来获取实际内容,并构造一个String。 Maybe that's causing the weird encoding issue? 也许这会导致奇怪的编码问题? Not sure. 不确定。

To retrieve the data I simply do: 要检索数据,我只需:

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

Since content is of type Text (app engine type) I use the .getValue() to get the actual result. 由于content是Text(app引擎类型)类型,我使用.getValue()来获取实际结果。 I don't think it's an issue with retrieving the data, since I can see the weird characters directly in the online app-engine datastore viewer. 我不认为这是检索数据的问题,因为我可以直接在在线应用程序引擎数据存储区查看器中看到奇怪的字符。

Are you using eclipse ? 你在用eclipse吗? if yes check under File > Properties > Text File encoding that your file is UTF-8 encoding. 如果是,请在文件>属性>文本文件编码下检查您的文件是否为UTF-8编码。

I would guess not. 我猜不会。

So, change it to UTF-8 and your issue should get fixed. 因此,将其更改为UTF-8,您的问题应该得到解决。

regards 问候

didier 迪迪埃

Followed this page to create a Servlet Filter so that all my pages were being encoded in utf8: 按照此页面创建一个Servlet过滤器,以便我的所有页面都在utf8中编码:

How to get UTF-8 working in Java webapps? 如何让UTF-8在Java webapps中运行?

After creating the filter, everything works! 创建过滤器后,一切正常!

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

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