aspose file tools*
The moose likes JSP and the fly likes Display SQL Server Image Data Type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Display SQL Server Image Data Type" Watch "Display SQL Server Image Data Type" New topic
Author

Display SQL Server Image Data Type

Mitchell Baron
Greenhorn

Joined: Jun 09, 2008
Posts: 6
Hi, I'm trying to display an image stored in a SQL Server 2000 image type database column. When the followinbg code is executed, the browser seems to find the image but only displays a RED X.

I understand that storing images in the database is not a good idea but do not have a choice in this case.

Does anyone have any suggestions I could try as I've never done this in Java/JSP?

Thank you!

int AccountID = commonNet.getInt(request.getParameter("AccountID"));
String strContentType = request.getParameter("strContentType");
if ((strContentType==null)||(strContentType.equals(""))) {
strContentType="image/gif";
}

byte[] imgData = null;
String strSQL = "";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

conn = globalNet.openDbConnection();
stmt = conn.createStatement();
strSQL = " SELECT imgPhoto " +
" FROM Contact " +
" WHERE AccountID = " + AccountID;
rs = stmt.executeQuery(strSQL);
if (rs.next())
{
imgData = rs.getBytes(1);
}
rs.close();
stmt.close();

response.setContentType(strContentType);
response.setContentLength(imgData.length);
OutputStream o = response.getOutputStream();
o.write(imgData);
o.flush();
o.close();


[ August 08, 2008: Message edited by: Mitchell Baron ]

[ August 08, 2008: Message edited by: Mitchell Baron ]
[ August 08, 2008: Message edited by: Mitchell Baron ]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You will be better off reading the image in from the database using rs.getBinaryStream() rather than the rs.getBytes() method.

An example might be:


This is mostly pseudo code, I haven't written one of these image servlets before, so the code isn't tested. But it shows most of the key points. I also don't know if setting the content-disposition for the images is necessary (I know it is for downloading files, so I kept it here...).


Steve
Mitchell Baron
Greenhorn

Joined: Jun 09, 2008
Posts: 6
Using rs.getBinaryStream() worked!!! It did not seem to need the content-disposition setting.

Many thanks for the suggestion!

Mitchell
[ August 10, 2008: Message edited by: Mitchell Baron ]
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Mitchell, you might find this tutorial on serving MIME content useful.


Fun programming etcetera!
Mitchell Baron
Greenhorn

Joined: Jun 09, 2008
Posts: 6
Thank you Dave. That is helpful!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display SQL Server Image Data Type