2017-02-10 132 views
-1

我正在通过以下Servlet方法从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

只需将它作为Web应用程序中的一个资源,可以使用JSP/Servlet访问。

实施例1固定的位置:

Servlet的位置:http://www.cvss.online/captcha

JSP代码:

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

实施例2动态位置由ID:

每个图像具有独特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(); 

干杯