java将Oracle blob类型数据导出为jpg图片

222 阅读1分钟

java程序将Oracle blob类型数据导出为jpg图片的一个小工具

Oracle版本:Oracle 11g

    /**
     * blob转jpg主方法
     *
     * @param args
     */
    public static void main(String[] args) {
        // 数据库配置
        String jdbcUrl = "jdbc:oracle:thin:@//127.0.0.1:1521/ngtest";
        String username = "nguser";
        String password = "123456";
        // 输出文件夹位置
        String outputDir = "F:\桌面\blob转jpg\outputTest";
        // 输出日志位置
        String logFilePath = "F:\桌面\blob转jpg\log.txt";
        // 查询sql
        String query = "SELECT * FROM TABLE_BLOB WHERE xxx = 'xxx'";
        // 开始处理
        try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) {
            // 执行sql
            PreparedStatement statement = connection.prepareStatement(query);
            // 获取结果Set
            ResultSet resultSet = statement.executeQuery();
            // 确保输出目录存在
            File outputDirFile = new File(outputDir, "/test/test");
            // 如果目录不存在则创建目录
            if (!outputDirFile.exists()) {
                outputDirFile.mkdirs();
            }
            
            while (resultSet.next()) {
                // 获取blob数据
                Blob blob = resultSet.getBlob("QP");
                if (blob != null) {
                    // 开启流写文件
                    try (InputStream inputStream = blob.getBinaryStream();
                         FileOutputStream outputStream = new FileOutputStream(new File(outputDir + "/test/test", "/output.jpg"))) {
                        byte[] buffer = new byte[1024];
                        int bytesRead;
                        while ((bytesRead = inputStream.read(buffer)) != -1) {
                            outputStream.write(buffer, 0, bytesRead);
                        }
                        // 写入成功
                        logMessage("Image saved successfully to {}", logFilePath);
//                        System.out.println("Image saved successfully to {}");
                    } catch (Exception e) {
                        // 写入失败
                        logMessage("Error saving image:" + e, logFilePath);
//                        System.out.println("Error saving image" + e);
                    }
                } else {
                    // blob类型数据为空
                    logMessage("Blob data is null for the current record", logFilePath);
//                    System.out.println("Blob data is null for the current record");
                }
            }
        } catch (Exception e) {
            logMessage("Database error:" + e, logFilePath);
//            System.out.println("Database error" + e);
        }
    }

    /**
     * 写日志
     *
     * @param message 日志信息
     * @param logFilePath 日志路径
     */
    private static void logMessage(String message, String logFilePath) {
        // 打开日志路径
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(logFilePath, true))) {
            // 写入日志信息
            writer.write(message);
            // 换行
            writer.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }