오라클

jsp BLOb 타입 다운로드

사라링 2013. 3. 13. 09:44

<%@page import="oracle.jdbc.driver.OracleResultSet"%>

<%@page import="com.base.SystemException"%>

<%@page import="java.io.*"%>

<%@page import="java.sql.*"%>


    <%


request.setCharacterEncoding("utf-8");

    response.reset();

    

    String conversation_ID  = request.getParameter("CLS_CONVERSATION_ID");

    String file_Seq = request.getParameter("CLS_FILE_SEQ");

Blob             emptyBlob = null;

OutputStream     outstream = null;

FileInputStream finstream = null;

ResultSet rs = null;

PreparedStatement pstmt = null;

Connection conn = null;


try {

javax.naming.Context env = new javax.naming.InitialContext();

javax.sql.DataSource source = (javax.sql.DataSource) env.lookup("baseDS");

conn = source.getConnection();

conn.setAutoCommit(false);

} catch (Exception e) {

throw new SystemException(e);

}

StringBuffer query  = new StringBuffer();


query.append("SELECT FILE_NAME, FILE_BINARY FROM SYN_XXSB_DCT_FILE WHERE CONVERSATION_ID = ? AND FILE_SEQ = ?  ");


try {

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

pstmt.setString(1, conversation_ID.trim());

pstmt.setString(2, file_Seq.trim());

rs = pstmt.executeQuery();

String fileName= "";

while(rs.next()){

fileName = rs.getString(1);

oracle.sql.BLOB blob = ((OracleResultSet)rs).getBLOB(2);

InputStream is = (blob.getBinaryStream());

String fileType = fileName.substring(fileName.indexOf(".")+1,fileName.length());

response.setContentType("application/x-msdownload"); 

response.setHeader("Content-Disposition", "attachment;filename="+fileName+";");

OutputStream os = response.getOutputStream();

int size = blob.getBufferSize();

byte[] buffer = new byte[size];

int length = -1;

while((length=is.read(buffer))!=-1){

os.write(buffer,0,length);

}

os.flush();

os.close();

is.close();

}

} catch (Exception e) {

throw new SystemException(e);

}finally{

try {if (rs != null)  rs.close(); } catch (Exception ex) {}

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

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

}



    

    

    %>