/**
* 加入附件
* @param String path
* @return
* @throws IOException
*/
public static byte[] insertBlob( String path) throws IOException{
byte[] buffer = new byte[1];
buffer[0] = 1;
InputStream in = null;
in = new FileInputStream(path);
byte[] b = new byte[(int) in.available()];
in.read(b);
return b;
}
/**
* 取出包含的附件
* @param Object obj, String path
* @return
* @throws SQLException
*/
public static void writeBlobByPath(Object obj, String path) throws IOException, SQLException{
// BLOB blob = (BLOB)obj;
byte[] blob = (byte[])obj;
InputStream ins = new ByteArrayInputStream(blob);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(path));
BufferedInputStream in = new BufferedInputStream(ins);
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
文件操作与数据库交互
本文介绍了如何使用Java进行文件读写操作,并将其转换为字节数组存储到数据库中,同时也展示了从数据库中读取字节数组并保存为文件的方法。文章包括了将文件插入数据库(Blob类型)及从数据库中提取文件的具体实现。
309

被折叠的 条评论
为什么被折叠?



