2017-02-10 4 views
-1

다음 서블릿 메소드를 통해 mysql 데이터베이스에서 이미지 바이트를 검색합니다.jsp에서 이미지를 표시하는 방법

이미지 바이트를 JSP 페이지에 표시합니다.

내 JSP 페이지에서 img 태그를 어떻게 사용합니까?

public byte[] getProfilePicture(int id) { 

    byte[] bytes = null; 

    try { 

     connection = dataSource.getConnection(); 
     pst = connection.prepareStatement("SELECT profile_picture FROM users WHERE id = '"+id+"' "); 
     resultSet = pst.executeQuery(); 

     while(resultSet.next()) { 

      bytes = resultSet.getBytes("profile_picture"); 

     } 

    } catch(Exception e) { 
     e.printStackTrace(); 
    } 

    return bytes; 

} 

덕분에, 나는 당신의 친절한 응답을 기다리고 있습니다.

답변

0

JSP/Servlet을 사용하여 액세스 할 수있는 웹 응용 프로그램 내 리소스로 만들면됩니다.

각 화상이 가지고 ID로

<div class="six columns"> 
    <label>Captcha image</label> 
    <img src="captcha"> 
</div> 

예 2 동적 위치 :

서블릿 위치 : http://www.cvss.online/captcha

JSP 코드

예 1 Loaction 수정 고유 ID

,
... 
    int imgId = Integer.parseInt(request.getParameter("imgId")); 
    // Your Logic 
    ... 

귀하의 예 : 당신의 resultSet.next 내

() 블록 추가 :

byte imageArray[] = rs.getBytes(1); 
response.setContentType("image/type"); //type png jif etc 
outputStream=response.getOutputStream(); 
outputStream.write(imageArray); 
outputStream.flush(); 
outputStream.close(); 

건배

관련 문제