2011년 11월 7일 월요일

BLOB 등등을 이미지로 웹화면에 출력


--- controller ---
         psmt.setString(1,user_id);
         
          rs = psmt.executeQuery();
         
          if(rs.next()) {
            emp_img = rs.getBinaryStream(1);
          }
        
         ModelAndView mav = new ModelAndView();       
         mav.setViewName("/jsp/dmce/edu/course/EduUserHistoryImage.jsp");
         mav.addObject("emp_img", emp_img);
         return mav;


--- jsp---
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.lang.String"%>
<%
InputStream emp_img = null;
emp_img = (InputStream) request.getAttribute("emp_img");
try {             
    response.setContentType("image/jpeg");
    out.clear(); //out--> jsp자체 객체
    out=pageContext.pushBody(); //out--> jsp자체 객체
     
    OutputStream os = response.getOutputStream();
    int binaryRead;
     
    int len=-1;
    byte [] byteRead = new byte[1024];
    while((len = emp_img.read(byteRead)) != -1) {
     os.write(byteRead,0,len);
    }
     
} catch (Exception ex) {
        ex.printStackTrace();
      throw new Exception("ERROR.\n"+ex.getMessage()); 
} finally {
      //return emp_img;
}
%>

---호출---
<img src="/edu/course/userCourse.do?method=showImageView"  height="119" width="98" />

댓글 없음:

댓글 쓰기