我用sql Server数据库,一个binary字段存储文章内容,在写入时:
int len = new String(request.getParameter("content").getBytes("UTF-8")).length();
//上面这里要区分文件编码,得到长度不一样的,如果用getBytes("ISO8859_1")得到的长度可能小。
String content=new String(request.getParameter("content").getBytes("ISO8859_1"),"GBK");
String sql="update ress_techfile set topic='" + topic + "',content=convert(binary("
+ len + "),'" + content + "'), edittime= '" + sqltime + "' where id='"+id+"' ";
cn.executeUpdate(sql);
这样写入,没有问题,但是在读取数据记录时:
int bytesum=0;
int byteread=0;
InputStream inStream=rs.getBinaryStream("content");
String content = "";
byte[] buffer = new byte[1024];
while ((byteread=inStream.read(buffer))!=-1)
{
bytesum += byteread;
content += new String(buffer);
}
content = content.subString(0,bytesum); //去的最后多余字节。
System.out.println("length=="+content.length()+"---data is---"+content);
//打印content的长度和内容,发现长度是和写入数据库的长度一致,可是内容后面却多了一截,不知是怎么回事啊,把内容复制出来用其他编辑器查看也发现长度多了,不知何解?
int len = new String(request.getParameter("content").getBytes("UTF-8")).length();
//上面这里要区分文件编码,得到长度不一样的,如果用getBytes("ISO8859_1")得到的长度可能小。
String content=new String(request.getParameter("content").getBytes("ISO8859_1"),"GBK");
String sql="update ress_techfile set topic='" + topic + "',content=convert(binary("
+ len + "),'" + content + "'), edittime= '" + sqltime + "' where id='"+id+"' ";
cn.executeUpdate(sql);
这样写入,没有问题,但是在读取数据记录时:
int bytesum=0;
int byteread=0;
InputStream inStream=rs.getBinaryStream("content");
String content = "";
byte[] buffer = new byte[1024];
while ((byteread=inStream.read(buffer))!=-1)
{
bytesum += byteread;
content += new String(buffer);
}
content = content.subString(0,bytesum); //去的最后多余字节。
System.out.println("length=="+content.length()+"---data is---"+content);
//打印content的长度和内容,发现长度是和写入数据库的长度一致,可是内容后面却多了一截,不知是怎么回事啊,把内容复制出来用其他编辑器查看也发现长度多了,不知何解?
楼主【wujunliang】截止到2008-07-30 23:53:05的历史汇总数据(不包括此帖):
发帖的总数量:8 发帖的总分数:200 每贴平均分数:25
回帖的总数量:8 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:7 结贴的总分数:180
无满意结贴数:4 无满意结贴分:240
未结的帖子数:1 未结的总分数:20
结贴的百分比:87.50 % 结分的百分比:90.00 %
无满意结贴率:57.14 % 无满意结分率:133.33%
楼主加油
取消马甲机器人,请点这里:http://www.java2000.net/mycsdn/robotStop.jsp?usern=wujunliang
http://zh.wikipedia.org/wiki/UTF-8