Study/Spring framework

blob 칼럼을 SqlRowset에서 이미지로 변경

nobang 2012. 10. 10. 15:35
728x90

Springframework에서 MS-SQL에 BLOB로 저장되어 있는 이미지를 불러와 파일로 저장

String sql = "select picture from users where id=?";
Object[] args = { userId };
Blob rsPhoto = (Blob)getJdbcTemplate().queryForObject(sql, args, Blob.class);


try {
            for(int i = 0; i < rsPhoto.length(); i++)
                {
                    String upDir = "c:\\imgs\\";
                    Random Number = new Random();
                    int Dice_number;
                    String strPath;
                   
                    Dice_number = Number.nextInt(999999);
                    strPath = upDir + Dice_number+".jpg";
                   
                    try {
                        logger.debug(rsPhoto == null ? "null " : "not null");
                        FileOutputStream newFile = new FileOutputStream(strPath);
                        newFile.write(rsPhoto.getBytes(1, (int)rsPhoto.length()));                       
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
        } catch (SQLException e) {
            e.printStackTrace();
        }

728x90