java效率问题 现在需要将数据库中的上千万条数据 写入txtjava将文件存入数据库 字符集 utf–8

学习总结(19)
1.首先查看数据库的设置:以mysql为例
在mysql的安装目录下的my.in文件内i修改设置
2.在servlet中设置
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
3.将jsp页面的表单提交方式更改为post
自从Tomcat5.x开始,GET和POST方法提交的信息,Tomcat采用了不同的方式来处理编码,对于POST请求,Tomcat会仍然使用request.setCharacterEncoding方法所设置的编码来处理,如果未设置,则使用默认的iso-8859-1编码。而GET请求则不同,Tomcat对于GET请求并不会考虑使用request.setCharacterEncoding方法设置的编码,而会永远使用iso-8859-1编码。
解决办法如下:
1.配置tomcat的配置文件server.xml里这句:
&Connector URIEncoding="GB2312"
port="8080"
maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" /&
加上这句:URIEncoding="GB2312"
2.使用String name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"GB2312");转化编码
推荐使用第二种方式。博客分类:
前几天在项目中需要读取用户上传过来的txt文件,但不确定txt文件的字符集
UTF-16、UTF-8(带BOM)、Unicode可以根据前三个字节区别
public String getTxtEncode(FileInputStream in) throws IOException{
byte[] head = new byte[3];
in.read(head);
String code = "GBK";
if (head[0] == -1 && head[1] == -2 )
code = "UTF-16";
if (head[0] == -2 && head[1] == -1 )
code = "Unicode";
if(head[0]==-17 && head[1]==-69 && head[2] ==-65)
code = "UTF-8";
if("Unicode".equals(code)){
code = "UTF-16";
但不带BOM的UTF-8和GBK前三个字节不确定,用以上方法无法区别
通过在google上搜索发现不带BOM的识别是Java遗留的一个bug,呵呵,终于找到根源了,Java提供了此bug的解决方案
package com.justsy.sts.utf8;
import java.io.*;
public class UnicodeInputStream extends InputStream {
PushbackInputStream internalIn;
boolean isInited = false;
String defaultE
private static final int BOM_SIZE = 4;
public UnicodeInputStream(InputStream in, String defaultEnc) {
internalIn = new PushbackInputStream(in, BOM_SIZE);
this.defaultEnc = defaultE
public String getDefaultEncoding() {
return defaultE
public String getEncoding() {
if (!isInited) {
} catch (IOException ex) {
IllegalStateException ise = new IllegalStateException(
"Init method failed.");
ise.initCause(ise);
protected void init() throws IOException {
if (isInited)
byte bom[] = new byte[BOM_SIZE];
n = internalIn.read(bom, 0, bom.length);
if ((bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00)
&& (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) {
encoding = "UTF-32BE";
unread = n - 4;
} else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)
&& (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) {
encoding = "UTF-32LE";
unread = n - 4;
} else if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB)
&& (bom[2] == (byte) 0xBF)) {
encoding = "UTF-8";
unread = n - 3;
} else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) {
encoding = "UTF-16BE";
unread = n - 2;
} else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) {
encoding = "UTF-16LE";
unread = n - 2;
encoding = defaultE
if (unread & 0)
internalIn.unread(bom, (n - unread), unread);
isInited = true;
public void close() throws IOException {
isInited = true;
internalIn.close();
public int read() throws IOException {
isInited = true;
return internalIn.read();
通过使用上述InputStream类的实现可以正确的读取出不带BOM和带BOM的字符集
package com.justsy.sts.utf8;
import java.io.BufferedR
import java.io.F
import java.io.FileInputS
import java.io.IOE
import java.io.InputStreamR
import java.nio.charset.C
public class UTF8Test {
public static void main(String[] args) throws IOException {
= new File("D:"+File.separator+"Order.txt");
FileInputStream in = new FileInputStream(f);
= Charset.defaultCharset().name();
UnicodeInputStream uin = new UnicodeInputStream(in,dc);
BufferedReader br = new BufferedReader(new InputStreamReader(uin));
String line = br.readLine();
while(line != null)
System.out.println(line);
line = br.readLine();
结合Java提供的方案,我们就可以比较完整的判别出各种字符集了
public String getTxtEncode(FileInputStream in) throws IOException{
= Charset.defaultCharset().name();
UnicodeInputStream uin = new UnicodeInputStream(in,dc);
if("UTF-8".equals(uin.getEncoding())){
uin.close();
return "UTF-8";
uin.close();
byte[] head = new byte[3];
in.read(head);
String code = "GBK";
if (head[0] == -1 && head[1] == -2 )
code = "UTF-16";
if (head[0] == -2 && head[1] == -1 )
code = "Unicode";
if(head[0]==-17 && head[1]==-69 && head[2] ==-65)
code = "UTF-8";
if("Unicode".equals(code)){
code = "UTF-16";
本文的转载地址为:
浏览: 64857 次
来自: 上海
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'博主最新文章
博主热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)关于JAVA没有中文字符串生成UTF-8文本文件问题
[问题点数:50分,结帖人feng1071]
关于JAVA没有中文字符串生成UTF-8文本文件问题
[问题点数:50分,结帖人feng1071]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
匿名用户不能发表回复!|Eclipse项目配置
在Eclipse的项目中,使用UTF-8编码。如果需要,最好把整个workspace都设置成UTF-8编码。
Tomcat配置
此处的配置主要是配置URL的配置,这样可以中文的URL了。在$Tomcat/conf/server.xml文件中添加高亮部分
&!-- Define a non-SSL HTTP/1.1 Connector on port 8080 --&
&Connector port=&8080& maxHttpHeaderSize=&8192&
maxThreads=&150& minSpareThreads=&25& maxSpareThreads=&75&
enableLookups=&false& redirectPort=&8443& acceptCount=&100&
connectionTimeout=&20000& disableUploadTimeout=&true&
URIEncoding=&UTF-8&
数据库配置
数据库多种多样,有Oracle、DB2、MySQL、SQLServer等常用的。不一一说明具体的数据库配置。
举例MySQL数据库来说。要设置的有数据库、表、字段。字符集全部设置成utf8编码,字符集整理一般使用utf8_gerenral_ci。
spring配置
在连接数据库上使用jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8来确保与数据库的交互使用UTF-8编码。
&bean id=&dataSource&
class=&org.apache.commons.dbcp.BasicDataSource&&
&!-- 配置数据源 --&
&property name=&driverClassName&
value=&com.mysql.jdbc.Driver&&
&/property&
&property name=&url&
value=&jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8&&
&/property&
&property name=&username& value=&root&&&/property&
&property name=&password& value=&root&&&/property&
jsp页面配置
第一行确保服务器传送一个UTF-8编码的流给客户端,第六行确保浏览器使用一个UTF-8编码来显示给客户。
&%@ page language=&java& import=&java.util.*& pageEncoding=&UTF-8&%&
&!DOCTYPE HTML&
&title&页面&/title&
&meta http-equiv=&Content-Type& content=&text/ charset=utf-8&&
Java Web应用配置
web.xml的文件配置
&filter-name&encodingFilter&/filter-name&
&filter-class&org.springframework.web.filter.CharacterEncodingFilter&/filter-class&
&init-param&
&param-name&encoding&/param-name&
&param-value&UTF-8&/param-value&
&/init-param&
&init-param&
&param-name&forceEncoding&/param-name&
&param-value&true&/param-value&
&/init-param&
声明:未经允许禁止转载 东东东
陈煜东的博客 文章,谢谢。如经授权,转载请注明: 转载自本文链接地址:
相关文章推荐:
Copyright & 2018

我要回帖

更多关于 java将数据库放入list 的文章

 

随机推荐