/**
* 加入附件
* @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();
}
本文介绍了一种从文件系统中读取数据并将其转换为字节数组的方法,同时也提供了一个将数据库中的Blob数据写入到指定路径的文件中的实现方案。这两种方法对于处理文件附件和数据库之间的数据交换非常实用。
309

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



