Here is a sample JSP, DealerPicture makes a connection to a (My)SQL DB and retrieves the images stored as BLOBs.
The one trick I learned was to not have any white space in your JSP, as soon as your JSP compiler outputs an out.write("\r\n"); your sunk.
<%@ page contentType="image/jpeg" %><%@ page import="cc.deeley.picture.DealerPicture" %><%@ page import="java.util.*" %><%@ pageimport="java.io.*" %><%@ page import="org.apache.log4j.Logger" %><%! private static Logger logger = Logger.getLogger("cc.deeley.picture"); %><%
int BLOCK_SIZE_WRITE = 512;
String picKey = request.getParameter("picnum");
int picInt =0;
if ((picKey == null) || (picKey.length() == 0)) {
picKey = "Missing picKey parameter";
}
try {
picInt = Integer.parseInt(picKey);
}
catch (NumberFormatException nfe) {
logger.error( " bad picKey: " +picKey);
}
DealerPicture dp = new DealerPicture(picInt);
dp.fetchPic( dp.getPicKey() );
if ( ( dp.getMime().indexOf("jpeg")!= -1 ) || ( dp.getMime().indexOf("jpg")!= -1 ) ){
response.setContentType("image/jpeg");
}
else if ( dp.getMime().indexOf("gif")!= -1 ) {
response.setContentType("image/gif");
}
OutputStream os = response.getOutputStream();
ByteArrayInputStream bs = new ByteArrayInputStream( dp.getPicMass() ) ;
try {
// fs = new FileOutputStream(f);
byte[] buf = new byte[BLOCK_SIZE_WRITE];
int bytes;
while ((bytes = bs.read(buf)) != -1) {
os.write(buf, 0, bytes);
}
}
catch (IOException ex) {
logger.error(ex.toString());
}
finally {
if (os != null) try {os.close();} catch (Exception ex) {}
}
%>