前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis读取Oracle数据库Blob字段,输出原文件

Mybatis读取Oracle数据库Blob字段,输出原文件

作者头像
程裕强
发布2022-05-06 20:43:34
8100
发布2022-05-06 20:43:34
举报

1、bean

代码语言:javascript
复制
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

代码语言:javascript
复制
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<AttachmentBean> getAttachmentList();
}

3、service

代码语言:javascript
复制
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<AttachmentBean> getAttachmentList() {
        return attachmentDao.getAttachmentList();
    }


    @Override
    public int parseAttachmentList() throws SQLException {
        int i=0;
        BLOB blob=null;
        List<AttachmentBean> 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;
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-06-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、bean
  • 2、dao
  • 3、service
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档