当前位置: 技术问答>java相关
为什么我从Oracle中longrow字段中取到本地的文件,总是乱吗呢?
来源: 互联网 发布时间:2015-07-06
本文导语: 我的一段测试代码 下面是文件存入数据库longrow的代码 File f = new File(fileurl); InputStream is = new FileInputStream(f); Sting sql = "update tt set t1 = ? where t2 = '1'"; PreparedStatement ps = conn.prepa...
我的一段测试代码
下面是文件存入数据库longrow的代码
File f = new File(fileurl);
InputStream is = new FileInputStream(f);
Sting sql = "update tt set t1 = ? where t2 = '1'";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setBinaryStream(1,is,(int)f.length() );
ps.executeUpdate();//将文件流插入到数据库中。
下面是我取文件,在本地创建文件的代码
InputStream is = null;
int size = 0;
ResultSet rs = stmt.executeQuery("select t1 from tt where t2 = '1'");
rs.next();
is = rs.getBinaryStream("t1") ;
int flen = is.available();
byte temp[] = new byte [flen];
int fread = is.read(temp);
File fileout = new File(fileurl);
fileout.createNewFile() ;
FileOutputStream fouts = new FileOutputStream(fileout);
fouts.write(temp) ;
文件存进去了一个Word文件,也取出来在本地创建了,但我打开word里面却是乱吗??
下面是文件存入数据库longrow的代码
File f = new File(fileurl);
InputStream is = new FileInputStream(f);
Sting sql = "update tt set t1 = ? where t2 = '1'";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setBinaryStream(1,is,(int)f.length() );
ps.executeUpdate();//将文件流插入到数据库中。
下面是我取文件,在本地创建文件的代码
InputStream is = null;
int size = 0;
ResultSet rs = stmt.executeQuery("select t1 from tt where t2 = '1'");
rs.next();
is = rs.getBinaryStream("t1") ;
int flen = is.available();
byte temp[] = new byte [flen];
int fread = is.read(temp);
File fileout = new File(fileurl);
fileout.createNewFile() ;
FileOutputStream fouts = new FileOutputStream(fileout);
fouts.write(temp) ;
文件存进去了一个Word文件,也取出来在本地创建了,但我打开word里面却是乱吗??
|
请问你的数据库charset是什么,有可能需要通过String的getBytes转换
|
用如下方法读取:
InputStream is = null;
int size = 0;
ResultSet rs = stmt.executeQuery("select t1 from tt where t2 = '1'");
rs.next();
is = rs.getBinaryStream("t1") ;
int flen = is.available();
byte temp[] = new byte [flen];
int fread = is.read(temp);
/**
还可以试用这种方法:
InputStreamReader ir=new InputStreamReader(is,"GB2312");
byte temp[] = new byte [flen];
int fread = is.read(temp);
*/
File fileout = new File(fileurl);
fileout.createNewFile() ;
OutputStreamWriter ow=new OutputStreamWriter(new FileOutputStream(fileout),"GB2312");
ow.write(temp) ;
//
InputStream is = null;
int size = 0;
ResultSet rs = stmt.executeQuery("select t1 from tt where t2 = '1'");
rs.next();
is = rs.getBinaryStream("t1") ;
int flen = is.available();
byte temp[] = new byte [flen];
int fread = is.read(temp);
/**
还可以试用这种方法:
InputStreamReader ir=new InputStreamReader(is,"GB2312");
byte temp[] = new byte [flen];
int fread = is.read(temp);
*/
File fileout = new File(fileurl);
fileout.createNewFile() ;
OutputStreamWriter ow=new OutputStreamWriter(new FileOutputStream(fileout),"GB2312");
ow.write(temp) ;
//