0

我正在开发consumes json由另一个 webapp 提供的应用程序。用于检索数据,然后将org.apache.http.client.HttpClient相关数据存储到 mysql 表中。

像这样的东西:

URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
HttpResponse httpResponse = client.execute(httpget);
rd = new BufferedReader(new InputStreamReader(httpResponse
    .getEntity().getContent(), "UTF-8"));
String line = "";
while ((line = rd.readLine()) != null) {
    //process it
}

响应标头

    HTTP/1.1 200 OK [Server: Apache-Coyote/1.1, Cache-Control: no-cache,no-store,private,must-revalidate,max-stale=0,post-check=0,pre-check=0, 
Expires: Thu, 01 Jan 1970 00:00:00 GMT, 
Pragma: no cache, 
Set-Cookie: JSESSIONID=26E4AC5C0EF37BA53B736DF9B0F903FD; 
Path=/, 
Content-Type: application/x-javascript;
charset=UTF-8, 
Transfer-Encoding: chunked, 
Date: Thu, 06 Jun 2013 12:10:06 GMT, 
Connection: close]

该代码适用于正常数据作为响应并填充相应的列。但在某些情况下,响应包含无法插入数据库表的垃圾字符。垃圾数据的一个这样的例子:

P . 1989-1990 ݋ ݇�� ���� �� ����J� ᣇ¹I� ������K� �PM�H�� ��옉� �I� ���� ܬ� �E� �������� ��  ���J� ��I�  �E� �� ��K� ܬ�芪�� ��A ��A�. �E����� ��� ��F�� ��M� ����� �������� �I�  �E� �� ��K�, ������ ����O� ��� ...

插入失败并出现令人讨厌的 SQLException:

java.sql.SQLException: Incorrect string value: '\xF1\xA3\x87\xB9I\xEF...' for column 'dataSet' at row 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:233)
    at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:818)
    at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:1)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:587)
    ... 5 more

关于如何将此类数据输入数据库表的任何建议?请指教。

一些平台信息:

OS: Ubuntu 12.04 LTS
MYSQL Version: 5.5.32 
//DB Details
character_set_client:latin1
character_set_connection:latin1
character_set_database:latin1
character_set_filesystem:binary
character_set_results:latin1
character_set_server:latin1
character_set_system:utf8

编辑

我想强调一个事实,如果将相同的代码和数据库服务器移到 Windows 环境中,它们可以正常工作。没有错误。但是,它们在 *nix 环境中失败。:(

4

1 回答 1

1

数据库将您的“垃圾数据”解释为文本而不是任意二进制数据。您可以通过对列使用二进制数据类型来避免这种情况dataSet,例如,VARBINARY代替VARCHAR,或BLOB代替TEXT

另见http://dev.mysql.com/doc/refman/5.5/en/binary-varbinary.html

于 2013-06-06T12:53:35.013 回答