我正在尝试检索BLOB数据(图像)并使用servlet在html页面中显示

时间:2018-05-24 08:40:02

标签: java mysql servlets blob

我正在尝试检索BLOB数据(图像)并使用servlet在html页面中显示。 以下是我的代码。但它什么都没显示。提前致谢。 另外我使用过MySQL数据库。

public class retriveLogo extends HttpServlet {
standardFunction sf = new standardFunction();
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session=request.getSession();
        Blob imageBlob=null;
        byte[] imgData = null;
    try {
            String sql1 = "select img from userLogo where userID=?";
            PreparedStatement statement = sf.co.con.prepareStatement(sql1);
            statement.setString(1, (String)session.getAttribute("User"));
            ResultSet resultSet = statement.executeQuery();
            while(resultSet.next())
            {
                imageBlob = resultSet.getBlob("doc");
                imgData = imageBlob.getBytes(1, (int) imageBlob.length());imageBlob.length());
            }
            OutputStream output = response.getOutputStream();
            response.setContentType("image/gif");
            output.write(imgData);
            output.flush();
            output.close();
        } catch(Exception e) {
            try {
                sf.co.con.rollback();
            } catch (SQLException ex1) {
                Logger.getLogger(insertPaperSize.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(insertPaperSize.class.getName()).log(Level.SEVERE, null, e);
        }
    }

我想称之为

<img src="retriveLogo">

0 个答案:

没有答案