在过去的几个小时里,我一直在努力弄清楚我的代码出了什么问题。这段代码一切正常,直到我收到一个包含日文字符的文件。Notepad++ 甚至一些在线实用工具都说文件的编码是UTF-8。记事本说它的UTF-8-BOM。我已经从文件中读取了我的数据并对其进行了处理,最后想将其写入数据库。
我收到错误 org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0xee My database encoding is UTF8 only..
package citynet.dta.pump;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;
import citynet.common.ServerException;
public class TestEncoding {
public static void main(String[] args) {
byte[] bytes = null;
try {
//use the below sql to create table 'testtable'
// create table testtable (text1 character varying, text2 character varying,text3 character varying)
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
DataOutputStream out = new DataOutputStream(baos);
out.writeBytes("INR,字仮名交じり文,3255104BTK1");
bytes = baos.toByteArray();
}
Class.forName("org.postgresql.Driver");
Connection c = DriverManager.getConnection("jdbc:postgresql://server:5432/dbname", "username", "password");
if (bytes != null) {
try (ByteArrayInputStream input = new ByteArrayInputStream(bytes)) {
String sql = "COPY testtable FROM stdin delimiter ',' NULL AS 'null' ENCODING 'UTF8' ";
BaseConnection pgcon = (BaseConnection) c;
CopyManager mgr = new CopyManager(pgcon);
try {
mgr.copyIn(sql, input);
} catch (SQLException ex) {
throw new ServerException("Error while copying data in Postgres DB:" + ex);
}
}
}
} catch (Exception e) {
System.out.println("Error:" + e);
}
}
}