-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplayImage.jsp
58 lines (47 loc) · 1.18 KB
/
displayImage.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<%@ page trimDirectiveWhitespaces="true" import="java.sql.*,java.io.*" %><%@ include file="jdbc.jsp" %><%
// Indicate that we are sending a JPG picture
response.setContentType("image/jpeg");
// Get the image id
String id = request.getParameter("id");
if (id == null)
return;
int idVal = -1;
try{
idVal = Integer.parseInt(id);
}
catch(Exception e)
{ out.println("Invalid image id: "+id+" Error: "+e);
return;
}
String sql = "SELECT albumImage FROM album WHERE albumId = ?";
try
{
getConnection();
Statement st = con.createStatement();
st.execute("USE orders");
st.close();
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(1,idVal);
ResultSet rst = stmt.executeQuery();
int BUFFER_SIZE = 10000;
byte[] data = new byte[BUFFER_SIZE];
if (rst.next())
{
// Copy stream of bytes from database to output stream for JSP/Servlet
InputStream istream = rst.getBinaryStream(1);
OutputStream ostream = response.getOutputStream();
int count;
while ( (count = istream.read(data, 0, BUFFER_SIZE)) != -1)
ostream.write(data, 0, count);
ostream.close();
istream.close();
}
}
catch (SQLException ex) {
out.println(ex);
}
finally
{
closeConnection();
}
%>