开发者

java convert to utf-8 from postgres

开发者 https://www.devze.com 2023-03-17 09:51 出处:网络
I\'m generating html files whit this data (stored in postgres): The html files are generated as UTF-8, but the string looks like they appear in the DB.

I'm generating html files whit this data (stored in postgres):

java convert to utf-8 from postgres

The html files are generated as UTF-8, but the string looks like they appear in the DB.

java convert to utf-8 from postgres

How I can do to make the text appear correctly? Like: Últiles de Escritorio

Note. I'm not able to change postgres configuration, I'm using Java 1.6, Postgres 8.4, 开发者_Python百科JDBC

UPDATE:

I use this code to create the html files:

public static void stringToFile (String file_name, String file_content) throws IOException {
    OutputStream out = new FileOutputStream(file_name);
    OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");

    try {
        try {
            writer.write(file_content);
        } finally {
            writer.close();
        }
    } finally {
        out.close();
    }
}

And I use it like:

StringBuilder html_content = new StringBuilder();

ResultSet result_set = statement.executeQuery(sql_query);

while (result_set.next()) {
    html_content.append(String.format('<li>%s</li>', result_set.getString(1)));
}

Utils.stringToFile('thehtmlfile.html', html_content.toString());

UPDATE: [SOLVED] This works for me:

new String(str.getBytes("ISO-8859-1"), "UTF-8")


I hope you're sure that the error isn't happening before you add the string to the db.

Because you can't change your db settings, this isn't so easy to handle, first of all you have to know in which format the text will be saved in your db, see here.

Than you should be a familiar with UTF16 see here and here.

Now, after you are familiar with codecs you wanna use, you have to create the correct utf16 value for each character you get from the db. I will just point out how it could work, the correct implementation you have to do by your own.

public char createUTF16char( char first, char second ) {
  char res = first;
  res = res << 8;
  res = res & (0x0F & second);
  return res;
}

This code should just combine the last 8bit of each char ( first and second ) to a new char. Maybe this is the operation you need but it depends on the coding what is used on the server.

Sincerely

0

精彩评论

暂无评论...
验证码 换一张
取 消