Case1-export image from blob field

Case1-export image from blob field

Requirement:

There is a single table that stores images (using Blob fields) and related information about the images. The requirement is to save the content of these Blob fields as image files from the database, with the file names being the related information of the images.

Environment

Database: Oracle Local: Windows Tool: Hutool-db module

Code

Database Configuration: src/main/resources/config/db.setting

# JDBC url, required
url = jdbc:oracle:thin:@localhost:1521/orcl
# Username, required
user = test
# Password, required. If the password is empty, please fill in pass = 
pass = test

Code: PicTransfer.java

public class PicTransfer {
    public static void main(String[] args) throws SQLException {
        Db.use().find(
                ListUtil.of("NAME", "TYPE", "GROUP", "PIC"), 
                Entity.create("PIC_INFO").set("TYPE", 1),
                rs -> {
                    while(rs.next()){
                        save(rs);
                    }
                    return null;
                }
        );
    }
    
    private static void save(ResultSet rs) throws SQLException{
        String destDir = "f:/pic";
        String path = StrUtil.format("{}/{}-{}.jpg", destDir, rs.getString("NAME"), rs.getString("GROUP"));
        FileUtil.writeFromStream(rs.getBlob("PIC").getBinaryStream(), path);
    }
}