将 oracle 数据库的blob字段 导出到磁盘保存为指定文件

将 oracle 数据库的blob字段 导出到磁盘保存为指定文件

代码如下(示例):

import oracle.sql.BLOB;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;

/**
 *  读取Oracl 数据库blob字段
 *  导出到本次磁盘 保存为jpg文件
 */
public class Test {

    private static String PIC_URL = "D:/img";
	private String sourceURL = "jdbc:oracle:thin:@ip:port:sid";
	
    public static void main(String args[]) {
        Test test = new Test();
        try {
            Map<String, String> allFileName = test.getAllFileName(PIC_URL);
            test.exportPic(allFileName);
            System.out.print("导出完成!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean exportPic(Map<String, String> allFileName) throws SQLException {
        Connection conn = null;
        Statement stt = null;
        ResultSet rs = null;


        try {

            Class.forName("oracle.jdbc.driver.OracleDriver");
            
            conn = DriverManager.getConnection(sourceURL, "username", "password");
            stt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

            // 查询blob字段
            String sql = "  SELECT  filename,blob FROM tablename ) ";
            rs = stt.executeQuery(sql);

            int i = 0;
            while (rs.next()) {
                try {
                    String name = rs.getString("filename");// 文件名
                    BLOB blob = (BLOB) rs.getBlob("blob");//文件

                    String filepath = PIC_URL + name + ".jpg";

                    if (!allFileName.containsKey(name + ".jpg")) {

                        FileOutputStream file = new FileOutputStream(filepath);
                        InputStream in = blob.getBinaryStream();
                        int len = (int) blob.length();
                        byte[] buffer = new byte[len]; // 建立缓冲区
                        while ((len = in.read(buffer)) != -1) {
                            file.write(buffer, 0, len);
                        }
                        file.close();
                        in.close();
                        System.out.println(++i + ":"+ filepath);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            rs.close();
            stt.close();
            conn.close();
        }

    }


    public Map<String, String> getAllFileName(String path) {
        File file = new File(path);
        File[] tempList = file.listFiles();
        Map<String, String> pathMap = new HashMap<>();
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                pathMap.put(tempList[i].getName(), null);
            }
        }
        return pathMap;
    }

}

这样就可以将blob字段导出到磁盘里
之后只需要看着文件夹是否有文件就可以了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值