기술/programming2008. 10. 10. 10:12
download download

Long Row 타입에 ORACLE DB에 이미지 파일을 일괄적으로 업로드 하는 프로그램
잠시 DB 마이그레이션 지원 나왔다가 필요하게되 급하게 작성해 봄.

10008032_이나영_0000011395400.JPG 이런 형식으로 된 이미지 파일을 일괄 업로드 하는 프로그램


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class img_upload {
    private Connection con;
    private String driverName = "oracle.jdbc.driver.OracleDriver";
    private String dbURL = "jdbc:oracle:thin:@00.21.00.0:1521:ORADB";

    public img_upload(String filename) {
        try {
            Class.forName(driverName);
            Connection con = DriverManager.getConnection(dbURL, "user", "pass");
           
            File dir = new File(filename);  -- 해당 디렉토리에 파일 목록을 가져 온다

            String[] files = dir.list(); -- 배열에 쏙 담아 두고.
           
            for(int i=0; i < files.length;i++){   -- 끝까지 마구 돌릴꺼다.
             String img_name = files[i];  -- 파일명을 가져와서
             int lst_idx = img_name.lastIndexOf("_"); -- 마지막 _를 찾는다.
             String reg = img_name.substring(lst_idx+1, lst_idx+14); -- 주민번호를 가저 오기 위해 가차 없이 잘라 버린다.
             String full_filename = filename + img_name; -- 전체 경로를 만들어
             
             File file = new File(full_filename);  -- 파일 객체에 담고
             int fileLength = (int)file.length();  -- 크기도 가져 오고
             
             InputStream fin = new FileInputStream(file); --스트림으로 읽어 버린다.

                PreparedStatement pstmt =
                    con.prepareStatement("update tb_img set img = ?, IMGLEN = ? where id = (select id from tb_mas where reg = ?)");
                pstmt.setBinaryStream(1, fin, fileLength); -- Long Raw 에 이미지를 넣는다
                pstmt.setInt(2, fileLength); -- 파일 크기도 넣어준다.
                pstmt.setString(3, reg); -- 그사람만 찾아서
                pstmt.executeUpdate(); -- 걍 실행 시켜 준다.
                
                pstmt.close(); -- 이걸 빼먹으면 커서수 초과했다는 에러를 만나보게 될것이다.
                
                System.out.println(i + full_filename);  -- 잘 올라가나 확인 해보자.
            }
            con.close();
           
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        catch(ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch(FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        if(args[0] == null) {
            System.out.println("사용법 : java [이미지 디렉토리]");
            System.exit(0);
        }
        img_upload fileup = new img_upload(args[0]);
    }
}

-----------------------------------------------------------------
테이블 스크립트

CREATE TABLE tb_IMG
(
  ID     VARCHAR2(32 BYTE)                    NOT NULL,
  IMGLEN   NUMBER(8),
  IMG      LONG RAW
)
TABLESPACE DAT
PCTUSED    0
PCTFREE    20
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          163M
            NEXT             1M
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
           )
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;

download download download
Posted by yachtie_leo