日期:2014-05-20  浏览次数:20617 次

如何保存一个InputStream流到sql2005某一字段

如何保存一个InputStream流到sql2005某一字段,我的字段名称为fileContent,类型为image
我的部分代码如下
rs=pstmt.executeQuery();
if(rs.next()){
Blob blob= rs.getBlob("FILECONTENT");
OutputStream outStream=blob.setBinaryStream(1l);
InputStream inStream=m.getFileContent1();//m.getFileContent1()得到InputStream流
// int size=blob.getBufferSize();
byte[] b=new byte[inStream.available()];
int n=0;
while((n=inStream.read(b))!=-1){
outStream.write(b, 0, n);
}
inStream.close();
outStream.close();
}

但是我读的时候
  String QuerySQL="select * from OA_MailFileInfo where fileid="+fileId;
  ResultSet rs=stmt.executeQuery(QuerySQL);
  if (rs.next()) {  
   
  Blob b = rs.getBlob("fileContent");  
  long size = b.length();  
}

size为0,我想肯定是保存到数据库时没有保存成功,哪位高手帮我解决下,不胜感激

------解决方案--------------------
http://zhidao.baidu.com/question/129752690.html

楼主看看这个
------解决方案--------------------
在查询之前把inStream insert into 到你指定表的字段里就好了啊??

------解决方案--------------------
去看java年鉴,各种大数据保存到各种数据库

http://www.herongyang.com/jdbc/SQL-Server-CLOB-setCharacterStream.html

到这里面找,再看看下面的链接
------解决方案--------------------
先把二进制流进行base64 编码,audioStr = new BASE64Encoder().encode(bs); 将btye【】变成String进行保存,取得时候用base64解码,注意得到的字符串最好不要拆分或添加什么,否则会出现乱码情况