2017-02-10 11 views
-1

myspaceデータベースから次のServletメソッドを使用してイメージバイトを取得します。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 /サーブレットを使用してアクセスできるWebアプリケーション内のリソースにするだけです。

例1固定Loaction:

サーブレット場所: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(); 

乾杯

関連する問題