oracle blob mybatis 存取

写入:


public void test(){

File file=new File("C:/Users/Administrator/Desktop/temp.pdf");
System.out.println("开始执行");
try{
InputStream is=new FileInputStream(file);//得到文件流
byte[] bytes = FileCopyUtils.copyToByteArray(is);//得到byte
Map<String,Object> file_name=new HashMap<>();
file_name.put("a", "temp2");
file_name.put("pdf_test", bytes);
ztestService.addPdfBlob(file_name);//添加到数据库中
}catch (IOException e) {  
       e.printStackTrace();  
}
System.out.println("执行结束");

}

}



SQL:

<select id="addPdfBlob" parameterType="Map" resultType="org.apache.commons.collections.map.CaseInsensitiveMap">
insert into z_test (a,pdf_test) values(#{a},#{pdf_test,jdbcType=BLOB})
</select>



读取:   //将blob字段读出 写入硬盘

public Map<String, Object> getPdfBlob(Map<String, Object> post) {
Map<String, Object> map = ztestMapper.getPdfBlob(post);//得到数据库返还的map
Blob blob = (Blob) map.get("pdf_test");//获取blob
Map<String, Object> fileMap = new HashMap<String, Object>();
try {
InputStream ins = blob.getBinaryStream();//创建读取blob流
//文件地址
File pathFile = new File(FileUtil.detectWebRootPath()+File.separator+"sourceFile"+File.separator+"temppdf");
if (!pathFile.exists()) {
pathFile.mkdirs();
}
// 输出到文件
String file_path=FileUtil.detectWebRootPath()+File.separator+"sourceFile"+File.separator+"temppdf"+File.separator+"a.pdf";
String file_src="sourceFile"+File.separator+"temppdf"+File.separator+"a.pdf";
File file = new File(file_path);
if (!file.exists())  {
file.createNewFile();
}
OutputStream fout = new FileOutputStream(file);
// 下面将BLOB数据写入文件
byte[] b = new byte[(int) blob.length()];
int len = 0;
while ((len = ins.read(b)) != -1) {
fout.write(b, 0, len);
}
// 依次关闭
fout.close();
ins.close();


fileMap.put("file", file_src);
} catch (IOException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return fileMap;


}



//想通过数据流的形式传入前台,但是受限于框架,无法完成,以下是测试代码

public Map<String, Object> getPdfBlob(Map<String, Object> post) {
Map<String, Object> a=new HashMap<String, Object>();
//a.put("a", "temp2");
Map<String, Object> map = ztestMapper.getPdfBlob(post);//得到数据库返还的map
Blob blob = (Blob) map.get("pdf_test");//获取blob
Map<String, Object> fileMap = new HashMap<String, Object>();
fileMap.put("file", blob);
return fileMap;
}

//blob转byte

private byte[] blobToBytes(Blob blob) {


BufferedInputStream is = null;


try {
is = new BufferedInputStream(blob.getBinaryStream());
byte[] bytes = new byte[(int) blob.length()];
int len = bytes.length;
int offset = 0;
int read = 0;


while (offset < len && (read = is.read(bytes, offset, len - offset)) >= 0) {
offset += read;
}
return bytes;
} catch (Exception e) {
return null;
} finally {
try {
is.close();
is = null;
} catch (IOException e) {
return null;
}
}
}


//百度到的解决方案

https://zhidao.baidu.com/question/1432545480249047779.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值