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

时间:2025-03-12 07:25:39

1、bean

package ;

import ;

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<AttachmentBean> getAttachmentList();
}

3、service

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .*;
import ;
import ;

@Transactional(propagation=,isolation=)
@Service("attachmentService")
@DataSource("ORACLE")
public class AttachmentServiceImpl implements AttachmentService {
    @Autowired
    private AttachmentDao attachmentDao;

    @Override
    public List<AttachmentBean> getAttachmentList() {
        return ();
    }


    @Override
    public int parseAttachmentList() throws SQLException {
        int i=0;
        BLOB blob=null;
        List<AttachmentBean> list=();
        for(AttachmentBean a:list){
            blob=(BLOB)();
            if(blob!=null) {
                i++;
                writeNewFile((), "D:\\a\\" + () + () + "." + ());
            }else{
                ("空附件:"+());
            }
        }
        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 = (buffer)) > 0) {
                (buffer, 0, len);
            }
        } catch (IOException e) {
            ();
        }
        try {
            ();
            ();
        } catch (IOException e) {
            ();
        }
        return true;
    }
    private boolean writeNewFile(byte[] data,String savePath){
        File file=new File(savePath);
        OutputStream os = null;
        try {
            os = new FileOutputStream(file);
            (data, 0, );
        } catch (Exception e) {
            ();
        }
        try {
            ();
        } catch (IOException e) {
            ();
        }
        return true;
    }
}