2015-08-28 101 views
1

我想从类型BLOB表中命名的图像和命名的图像列中检索图像,但运行代码时它给像这个 -从Oracle数据库中检索图像,并显示在JSP图像

团块代码“ [email protected]

请参阅我的代码获取和显示在jsp页面/浏览器中的图像: -

ResultSet resultset = 
    statement.executeQuery("select * from image where id = '" + id + "'") ; 

浏览器页面: -

<TABLE border="1"> 
     <TR> 
      <TH>ID</TH> 
      <TH>picture</TH> 
     </TR> 
     <TR> 
      <TD> <%= resultset.getString(1) %> </TD> 
      <TD> <%= resultset.getBlob(2) %> </TD> 
     </TR> 
    </TABLE> 
    <BR> 
+0

尝试使用'resultSet.getBinaryStream()回应的图像。' –

回答

1

第一:

[email protected]toString()是方法的响应,而不是图像。

二:

ResultSet resultset = 
    statement.executeQuery("select * from image where id = '" + id + "'") ; 

SQL注入的缘故,请!

最后
你不能回应image与心脏text数据!必须通过分离的cgi/servlet来检索图像,其中内容类型设置为image,因此适用于您的情况。

<TD> <%= resultset.getString(1) %> </TD> 
<TD> <img src="/get_img?id="<%=id%>/> </TD> 

,以后你需要一个servlet/CGI为get_img

@WebServlet(name = "img", urlPatterns = {"/get_img"}) 
public class get_img extends HttpServlet{ 
doGet(HttpServletRequest req, HttpServletResponse resp){ 
... 
resp.setContentType("image/jpg");//or image/whatever 
    try(ResultSet rs=ps.executeQuery()){//where ps is a PreparedStatement 
    //set the content length, and respond image data 
    //example: http://stackoverflow.com/a/4332975/5266804 
    } 
... 
} 
}