하은양 믿음군 효실맘 호홍홍집s

oracle BLOB로 저장된 file을 서버에 저장하기 본문

가벼운 배움/jsp

oracle BLOB로 저장된 file을 서버에 저장하기

호홍홍집 2016. 6. 9. 10:56

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import = "oracle.jdbc.driver.OracleResultSet"%>
<%@ page import = "java.sql.*" %>
<%@ page import = "java.sql.DriverManager" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.Statement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "java.sql.Timestamp" %>
<%@ page import = "java.io.*" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.util.*" %>
<%@ page import = "java.text.*" %>
<%@ page import = "java.net.URLConnection"%>
<%@ page import = "java.net.HttpURLConnection"%>
<%@ page import = "java.net.URL"%>
<%@ page import = "java.net.URLEncoder"%>
<%@ page import = "org.springframework.context.ApplicationContext"%>
<%@ page import = "org.springframework.context.support.ClassPathXmlApplicationContext"%>
<%@ page import = "javax.sql.DataSource"%>
<%
 // 원본 DB
 String SrcDriver = "jdbc:oracle:thin:@XXX.XXX.XXX.XXX:1521:ORCL";  // 소스 DB driver
 String SrcUser   = "";  // DB USer
 String SrcPass  = "";  // DB Password
 Connection  SrcConn = null;
 Statement  SrcStmt = null; 

 /* 고정값 RFC DB */
 ApplicationContext context = new ClassPathXmlApplicationContext("datasource.xml");
 DataSource ds = (DataSource)context.getBean("IamDataSource");
 Connection  DesConn = null; 
 Statement  DesStmt = null;  
 PreparedStatement destPsmt = null;
 ResultSet  rs = null;
 ResultSet  rs2 = null;
 ResultSet  rs3 = null;
 ResultSet  rs4 = null;
 
 Class.forName("oracle.jdbc.driver.OracleDriver");
 String sSql = "";

 InputStream is = null;
 FileOutputStream fos = null;

 try {
  try {
    
   SrcConn = DriverManager.getConnection(SrcDriver, SrcUser, SrcPass);   
   if(SrcConn != null) SrcStmt = SrcConn.createStatement();      
   
  } catch(SQLException sqlSubEx) {
   out.print("sql sub error::"+sqlSubEx);

  }finally{   
   out.println("SrcConn-start::"+SrcConn+"<br/>");
   out.println("SrcStmt-start::"+SrcStmt+"<br/>");   
  }
  
  String sTableName = "";
  String saveFilePath = "/home/www/upload_data/board_data/";

  // Get File NameList  
  sSql = "SELECT HPAGE.NAME, HPAGE.FILE_NAME,HPAGE.BLOB_CONTENT,HPAGE.DOC_SIZE " +
   " FROM CM8200T LAW,  HPAGE_UPLOAD_FILE HPAGE " +
   " WHERE LAW.FULL_FILE_NAME = HPAGE.NAME " +
   " ORDER BY LAW.LAW_ID, LAW.YMD, LAW.FILE_SNO ";
  rs = SrcStmt.executeQuery(sSql);
  int countFileID = 0;
  while(rs.next()){
   String sFileID = rs.getString("NAME");  if(sFileID == null) sFileID = "";
   String sFileName = rs.getString("FILE_NAME"); if(sFileName == null) sFileName = "";
   oracle.sql.BLOB blob = ((OracleResultSet)rs).getBLOB("BLOB_CONTENT");
   is = (blob.getBinaryStream());
   fos = new FileOutputStream(saveFilePath+countFileID+sFileName);   // 복사위치

   int size = blob.getBufferSize();
   byte[] buffer = new byte[size];
   int length = -1;

   while((length=is.read(buffer))!=-1){
    fos.write(buffer,0,length);
   }

   fos.close();
   is.close(); 
   
   countFileID++;
   
  }
  if(rs != null) try { rs.close(); } catch(SQLException sqlSubEx) {} 
 
 } catch(SQLException sqlEx) {
  out.print("sql error::"+sqlEx);
 
 }catch(Exception ex){
  out.println("sSql::" + sSql + "<br />");
  out.println("error::" + ex);

 }finally{  
  if (fos != null) try { fos.close(); } catch(Exception ex) {}    
  if (is != null) try { is.close(); } catch(Exception ex) {}
  
  if (rs != null) try { rs.close(); } catch(SQLException ex) {}      
  if (rs2 != null) try { rs2.close(); } catch(SQLException ex) {}      
  if (rs3 != null) try { rs3.close(); } catch(SQLException ex) {}      
  if (rs4 != null) try { rs4.close(); } catch(SQLException ex) {}      
    
  if (SrcStmt != null) try { SrcStmt.close(); } catch(SQLException ex) {}    
  if (DesStmt != null) try { DesStmt.close(); } catch(SQLException ex) {}
  if (destPsmt != null) try { destPsmt.close(); } catch(SQLException ex) {}
    
  if (SrcConn != null) try { SrcConn.close(); } catch(SQLException ex) {}
  if (DesConn != null) try { DesConn.close(); } catch(SQLException ex) {}  
 } 
%>