博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mybatis读取Oracle数据库Blob字段,输出原文件
阅读量:2389 次
发布时间:2019-05-10

本文共 4291 字,大约阅读时间需要 14 分钟。

1、bean

package com.cntaiping.tpa.bean;import java.sql.Blob;public class AttachmentBean {
private Integer id; private String finename; private Long contentSize; private String fileType; //Java的Object类型来对应数据库的BLOB类型,后边将Object转化成Blob类型 private Object content; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getFilename() { return finename; } public void setFileName(String finename) { this.finename= finename; } public Long getContentSize() { return contentSize; } public void setContentSize(Long contentSize) { this.contentSize = contentSize; } public String getFileType() { return fileType; } public void setFileType(String fileType) { this.fileType = fileType; } public Object getContent() { return content; } public void setContent(Object content) { this.content = content; }}

2、dao

package com.cntaiping.tpa.dao;import com.cntaiping.tpa.bean.AttachmentBean;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Result;import org.apache.ibatis.annotations.Results;import org.apache.ibatis.annotations.Select;import java.util.List;public interface AttachmentDao {    @Select("select ID,FILENAME,CONTENTSIZE,FILETYPE,CONTENT from attachment")    @Results({            @Result(column="ID",property="id"),            @Result(column="FILENAME",property="filename"),            @Result(column="CONTENTSIZE",property="contentSize"),            @Result(column="FILETYPE",property="fileType"),            @Result(column="CONTENT",property="content"),    })    List
getAttachmentList();}

3、service

package com.cntaiping.tpa.service.impl;import com.cntaiping.tpa.bean.AttachmentBean;import com.cntaiping.tpa.dao.AttachmentDao;import com.cntaiping.tpa.dao.datasource.DataSource;import com.cntaiping.tpa.service.AttachmentService;import oracle.sql.BLOB;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Isolation;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import java.io.*;import java.sql.SQLException;import java.util.List;@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)@Service("attachmentService")@DataSource("ORACLE")public class AttachmentServiceImpl implements AttachmentService {
@Autowired private AttachmentDao attachmentDao; @Override public List
getAttachmentList() { return attachmentDao.getAttachmentList(); } @Override public int parseAttachmentList() throws SQLException { int i=0; BLOB blob=null; List
list=attachmentDao.getAttachmentList(); for(AttachmentBean a:list){ blob=(BLOB)a.getContent(); if(blob!=null) { i++; writeNewFile(blob.getBinaryStream(), "D:\\a\\" + a.getObjId() + a.getName() + "." + a.getFileType()); }else{ System.out.println("空附件:"+a.getName()); } } return 0; } private boolean writeNewFile(InputStream in,String savePath){ File file=new File(savePath); OutputStream os=null; try { os= new BufferedOutputStream(new FileOutputStream(savePath)); int len = 0; byte[] buffer = new byte[1024]; while ((len = in.read(buffer)) > 0) { os.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } try { os.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } return true; } private boolean writeNewFile(byte[] data,String savePath){ File file=new File(savePath); OutputStream os = null; try { os = new FileOutputStream(file); os.write(data, 0, data.length); } catch (Exception e) { e.printStackTrace(); } try { os.close(); } catch (IOException e) { e.printStackTrace(); } return true; }}

转载地址:http://ievab.baihongyu.com/

你可能感兴趣的文章
网友将电视剧潜伏当职场教科书 研究办公室政治
查看>>
graudit
查看>>
使用Hudson和FindBugs进行持续集成和代码检查
查看>>
New Tool: The PenTesters Framework (PTF) Released
查看>>
Detecting and Defending against PowerShell Shells
查看>>
NagVis实物监控工具
查看>>
nginx - low risk webdav destination bug
查看>>
Lessons Learned from Building and Running MHN, the World's Largest Crowdsourced Honeynet
查看>>
Logwatch Linux/Unix系统日志检测软件
查看>>
减少Linux下Squid服务器的TIME_WAIT套接字数量
查看>>
/etc/sudoers中的含义
查看>>
Five must-know open source SDN controllers
查看>>
Finding Bad Guys with 35 million Flows, 2 Analysts, 5 Minutes and 0 Dollars
查看>>
SANS FOR572 Logstash
查看>>
apt成熟度模型
查看>>
Digital Forensics Framework v0.4.3 available
查看>>
linux设置bond网卡绑定
查看>>
Is your .svn showing (like 3300 other sites)?
查看>>
PCI DSS Update Could Include Virtualization Security(转载自baoz)
查看>>
List of Windows Auto Start Locations
查看>>