我正在制作一个学校项目(网站),需要将图像文件上传到mysql,然后将其检索并使用jsp,html将其存储到某种表中。我知道有答案here。我试图看一下,但无法完全理解。请帮助我。
这是我的代码
<%@ include file = "connect.jsp" %>
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<table>
<tr><td style="width: 50px">
<%@ page import = "image.*" %>
<%@ page import = "java.io.*" %>
<%
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/trainmore", "root","");
Statement stmt = conn.createStatement(1004,1008);
ResultSet rs1;
rs1 = stmt.executeQuery("select image from demoimage where id = 1");
if (rs1.next()) {
byte[] imgData = rs1.getBytes("image");
System.out.println(imgData);
response.setHeader("expires", "0");
response.setContentType("image/jpg");
OutputStream os = response.getOutputStream();
os.write(imgData);
os.flush();
os.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
%>
</td></tr>
</table>
</body>
</html>
我成功检索了图像,但是它是一整页,并且具有黑色背景,如this。很难看到,但是我叫的图像在那里。我该怎么办?谢谢