aspose file tools*
The moose likes JSP and the fly likes Problem with displaying image using jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Problem with displaying image using jsp" Watch "Problem with displaying image using jsp" New topic
Author

Problem with displaying image using jsp

Honour Cooker
Greenhorn

Joined: Aug 16, 2004
Posts: 10
Hello everyone,

I have searched all the information about how to display images from database using jsp in this forum, but none of the problems is samilar to mine.

I used a servlet class to retrieve the image blob data and wrote it to the response outputstream.

-------------------------------------------------------------------
String getResourceSql = "select content from tblContent where contentID = 9";
ResultSet docRs = stat.executeQuery(getResourceSql);

if (docRs.next()){
byte[] doc = (byte[])(docRs.getObject("content"));
System.out.println("data length " + doc.length);
ServletOutputStream ostr = response.getOutputStream();
ostr.write(doc);
ostr.flush();
ostr.close();
System.out.println("test");
}

---------------------------------------------------------------------

And after, I wrote a jsp file to call the servelt to display the image as below:

<html>
<body>

<img src="/meg/ImageServlet?contentId=8"/>

</body>
</html>

I could get the servlet test message (data length and "test" string ) printed out in Tomcat dos window, which means that the servlet was called successfully. However, the image could not be displayed in the jsp page (only a red cross). I wonder what is wrong with my code?

Thanks a lot in advance!
Honour Cooker
Greenhorn

Joined: Aug 16, 2004
Posts: 10
Sorry, a little typing mistake from my provious message. The contentID in my jsp page shouldn't be important as I already hardcoded it in the servlet class.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Do you set the content type or any other header information?

Jules
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Honour,

The following simple example works perfectly for me:

Perhaps you could adapt it to your needs.

Jules
Honour Cooker
Greenhorn

Joined: Aug 16, 2004
Posts: 10
Hi Julian,

Thank you very much for help. I have replaced your sample code with my database code. It worked good as gold.

My problem is to extract image blob from database not a local file. Inspired by you, I tried to extract blob and saved it as a gif file same as your way to see if the jsp page show image or not. Strangly, the saved gif file even could not be opened by photoshop(jsp too). So I suspect the reason why jsp can not show blob image from database is that the data format has been changed somehow during inserting and retriving to/from database. I don't know why it could happen. By the way, I only set one response header:response.setContentType = "image/gif".

Do you have any ideas? Thanks a lot!

Honour
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
OK, firstly are you sure the data you're writing to the database in the first place are correct?

Secondly, if you post your database writing and reading code snippets in a new thread on the JDBC forum here you're likely to get more help. Let us know which database and JDBC driver you're using too.

Jules
Honour Cooker
Greenhorn

Joined: Aug 16, 2004
Posts: 10
This is upload.jsp I use to save image to mysql database. I am using com.mysql.jdbc.Driver

----------------------------------------------------
<!-- upload.jsp -->
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>

<%
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {

DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();

byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
out.println("Driver Loaded!");
} catch (Exception E) {
out.println("Unable to load driver.");
}

try {
String connectionURL = "jdbc:mysql://localhost:3306/meg?user=;password=";
Connection C = DriverManager.getConnection(connectionURL,"","");
C.setAutoCommit(false);

String insert = "insert into tblContent (content) values (?)";

PreparedStatement pstmt = C.prepareStatement(insert);
pstmt.setObject(1, dataBytes);
pstmt.executeUpdate();

pstmt.close();
C.close();
out.println("Inserted row " );

} catch (SQLException E) {
out.print( "SQLException: " + E.getMessage());
} catch (Exception E) {
out.print("Error " + E.toString());
}



}
%>

-------------------------------------------------------
Thanks!
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
pstmt.setObject(1, dataBytes); probably won't help matters. Try using setBytes() instead. That may do the trick.

Jules
Honour Cooker
Greenhorn

Joined: Aug 16, 2004
Posts: 10
Thanks. The problem is still not fixed. I could see the binary data saved in table via phpAdmin. The saved in and retrieved out binary length are the same. I am going to take another way to save the file path to database instead.
 
wood burning stoves
 
subject: Problem with displaying image using jsp