Мне нужно получить изображение из БД MySQL.
Я получил фрагмент кода откуда-то, но не смог правильно отобразить изображение на панели jQuery.
Код запускается на новой странице JSP.
Может кто-нибудь сказать мне, как эффективно использовать outputtream в моем приложении?
Код, который у меня есть:
<% @page import = "java.sql.*" %>
<% @page import = "java.io.*" %>
<% Blob image = null;
Connection con = null;
byte[] imgData = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/aes", "root", "password");
stmt = con.createStatement();
rs = stmt.executeQuery("select PHOTO from tab_1 where name ='" + name + "'");
if (rs.next()) {
image = rs.getBlob(1);
imgData = image.getBytes(1, (int) image.length());
} else {
out.println("image not found for given id>");
return;
}
// display the image
response.setContentType("image/gif");
OutputStream o = response.getOutputStream();
o.write(imgData);
o.flush();
o.close();
} catch (Exception e) {
out.println("Unable To Display image");
out.println("Image Display Error=" + e.getMessage());
return;
} finally {
try {
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
%>