取回图像后无法显示图像(中等斑点)

时间:2018-07-25 07:32:16

标签: image jsp servlets blob mysql-workbench

我从YouTube上获取了此代码,从插入到检索都进行得很好,但是检索完成后图像不会出现。它仅显示图像已损坏。

<%@page import="controller.FileDAO"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.Blob"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%
String id = request.getParameter("id");
try {
    Connection con = FileDAO.getConnection();
    PreparedStatement ps = con.prepareStatement("select * from test where image_id=?");
    ps.setString(1, id);
    ResultSet rs = ps.executeQuery();
    if (rs.next()) {
        Blob blob = rs.getBlob("image");
        byte byteArray[] = blob.getBytes(1, (int) blob.length());
        response.setContentType("image/*");
        OutputStream os = response.getOutputStream();
        os.write(byteArray);
        os.flush();
        os.close();
    } else {
        System.out.println("No image found with this id.");
    }
} catch (Exception e) {
    out.println(e);
}
%>

在youtube上发布此内容的人没有这个问题吗,那么有帮助吗?

0 个答案:

没有答案