JAVA和SQL关于BLOB的很大的问题?……

来源:百度知道 编辑:UC知道 时间:2024/06/24 11:07:21
使用Blob对象保存声音文件到数据库,现在省去往数据库中插入数据一步而是将要保存的对象保存在Blob对象这中,然后再从这个Blob对象中取出声音文件保存在硬盘上。实现代码如下:
File finput = new File("e:\\214.mp3");
FileInputStream stream = new FileInputStream(finput);

ByteArrayOutputStream out = new ByteArrayOutputStream(1000);

byte[] b = new byte[1000];
byte[] t=new byte[stream.available()];
int n,k=0;
while ((n = stream.read(b)) != -1) {
out.write(t, k, n);
k += n;
}
out.toByteArray();
stream.close();
out.close();

/*java.sql.Blob*/
Blob blob=new SerialBlob(t);

InputStream inputStream = blob.getBinaryStream();

File foutput=new File("e:\\215.mp3");
FileOutputStream fo = new FileOutputStream(foutput);

int c;
// 读取流并写入到文件中
while ((c = inputStream.read()) != -1) {
fo.write(c);
}
fo.close()

Blob是保存二进制数据的,一般用于保存图片的。至于用于保存声音的项目我还没有做过,所以不清楚;
但从保存二进制编码发生错误来说,我觉得是格式问题;建议把MP3格式转为其他格式看看如何。我见到的通过web进行音频保存都是wmv格式,或者更加小的格式,好小是mp3的。
纯属个人意见,不知道对不对的。