java--数据库BLob字段内容往客户端写文件,例如数据库有一附件的BLob字段值,需要往客户端的C盘下写文件。
求思路
客户端是通过浏览器访问服务端的吗?
浏览器中不能直接操作客户的磁盘,除非是通过applet, flash, 或者其他插件来向客户端写文件
java.sql.Blob blob=obj.getBlob();
InputStream in = blob.getBinaryStream();
以上代码可以将blob类型转换为IO流,IO流可以写文件了吧。
[code="java"]
import java.sql.Blob;
//先从数据库里读出Blob字段
Blob mesg_data = (Blob) db.get("MESG_DATA");
//读Blob中的数据
InputStream is = mesg_data.getBinaryStream();
// 将mesg_data字段内的数据写入mesg_data.xml
File file = new File("C:/mesg_data.xml");
OutputStream os = new FileOutputStream(file);
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
fout.write(b, 0, len);
}
os.close();
is.close();
[/code]
简单的代码,从数据库取数据,异常你自己处理,希望有所帮助!
[code="java"]os.write(b, 0, len); [/code]