【问题标题】:Retrieve image from oracle database and show image in jsp从oracle数据库中检索图像并在jsp中显示图像
【发布时间】:2015-11-23 01:20:07
【问题描述】:

我想从名为 Image 的表和名为 image 且类型为 blob 的列中检索图像,但是在运行代码时,它会给出这样的 blob 代码 -

“oracle.sql.BLOB@193242d”

请参阅我的代码以获取并在 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>

【问题讨论】:

  • 尝试使用resultSet.getBinaryStream().

标签: oracle jsp jdbc blob


【解决方案1】:

第一:

oracle.sql.BLOB@193242d 是toString() 方法的响应,而不是图像。

第二:

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

为了SQL注入,请!

终于
你不能用text的数据回复image!必须通过内容类型设置为 image 的单独的 cgi/servlet 检索图像,所以对于您的情况。

<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
  }
...
}
}

【讨论】:

    【解决方案2】:

    您可以创建一个 servlet 来提供来自 DB 的图像,请参阅 How to retrieve images from database and place on JSP?

    或者您可以将图像数据直接嵌入到图像标记的src 属性中:I want to retrieve image from mysql database using servlet and show that image along with that user details in table - 尽管这仅推荐用于小图像(图标等)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-02
      • 1970-01-01
      相关资源
      最近更新 更多