본문 바로가기

개발

이미지 파일을 DB에 넣기

프로젝트에서 이미지 파일을 db에 넣을 일이 생겨서 한참을 찾았넹.ㅋ
하고나니 어렵지 않은데,,,, 네이버에는 검색안되고... 구글에서 겨우 찾았다.
oracle은 많은데 MS-SQL은 별로 없다. 우리나라는 넘 오라클을 좋아해...ㅋㅋㅋㅋ

그리고 구현되어 있는 방법도 모두 동일..... 다양성이 너무 부족해..



Language : JAVA
Database : MS-SQL
TableName : GMSPHOTOM

FieldName(DataType) : PHOTO(image), THUMNAIL(image) ...

        Connection conn = null;
        PreparedStatement pstmt = null;
        StringBuffer sql = null;
        
        int result = 0;
        
        try {
        	
        	File photo_temp = new File(PHOTO_TEMP_PATH+file_nm);
        	
        	if (photo_temp != null && photo_temp.isFile() && photo_temp.length() != 0) {
        	
        	// create thumnail	
            	createThumnail(file_nm, ad_no);
            	
                File photo = new File(PHOTO_PATH, ad_no+".jpg");
                File thumnail = new File(PHOTO_PATH, ad_no+"_t.jpg");
                
                if (ad_no != null && !ad_no.equals("") && photo != null && photo.length() != 0) {
                
                	InputStream fis_photo = new FileInputStream(photo);
                    InputStream fis_thumnail = new FileInputStream(thumnail);
                    
                    // photo delete
                    delete(ad_no, db_prefix_nm, dblink_prefix_nm, conn);
                    
                    // photo insert : empty image
                    sql = new StringBuffer();
                    sql.append("INSERT INTO ").append(db_prefix_nm).append("GMSPHOTOM")
                       .append(dblink_prefix_nm)
                       .append(" (AD_NO, CRT_DT, CRT_ID, CRT_IP, PHOTO, THUMNAIL ) ")
                       .append("  SELECT ? ,GETDATE(), ?, ?, ?, ? ");
                    
                    conn = getConnection();

                    pstmt = conn.prepareStatement(sql.toString());

                    pstmt.setString(1, ad_no);
                    pstmt.setString(2, user_id);
                    pstmt.setString(3, user_ip);
                    pstmt.setBinaryStream(4, fis_photo, (int) photo.length());
                    pstmt.setBinaryStream(5, fis_thumnail, (int) thumnail.length());
                    
                    pstmt.executeUpdate();
                    
                    fis_photo.close();
                    fis_thumnail.close();

                }
                
                conn.setAutoCommit(true);
                conn.commit();

                result = 1; // success
        	} 

        } catch (Exception e) {
            conn.rollback();
            e.printStackTrace();            
        } finally {

            if(pstmt != null) try{pstmt.close();}catch(Exception ignore){}
            if(conn != null) try{conn.close();}catch(Exception ignore){}

        }
        return result;
        



728x90

'개발' 카테고리의 다른 글

Getting AbstractMethodError when calling setBinaryStream  (0) 2012.08.10
[juno] svn plugin  (0) 2012.07.17
[단축키] Ctrl+Alt+i  (0) 2012.07.17