venu101
asked on
Image from a database
How can i retrieve an image from database and display in jsp page?
While rs.next() {
}
While rs.next() {
}
ASKER
<%
InputStream gifdata = null;
String fullName = null;
try{
Class.forName("oracle.jdbc .driver.Or acleDriver ");
DriverManager.registerDriv er(new oracle.jdbc.driver.OracleD river());
Connection con= DriverManager.getConnectio n("jdbc:or acle:thin: ");
Statement stmt1 = con.createStatement();
String query = "select IMAGE FROM IMAGES";
PreparedStatement pst = con.prepareStatement(query );
// bind the parameter with code value
// Obtain the result-set for the selected airline record
ResultSet result = pst.executeQuery();
// get user home folder name
if( result.next() ) {
gifdata = result.getBinaryStream(1);
// create new file
File gifFile = new File(fullName);
// Write the byte array into a local file
FileOutputStream file= new FileOutputStream(gifFile);
int chunk=0;
// write to the local file until image (LONGRAW) data is found
while( (chunk = gifdata.read()) != -1) {
file.write(chunk);
}
// flush the data
file.flush();
file.close(); // close the file
}
}
catch(Exception e) {
out.println("Exception :: "+ e);
}
Its giving me java.lang.NullPointerExcep tion
could anyone correct me please?
InputStream gifdata = null;
String fullName = null;
try{
Class.forName("oracle.jdbc
DriverManager.registerDriv
Connection con= DriverManager.getConnectio
Statement stmt1 = con.createStatement();
String query = "select IMAGE FROM IMAGES";
PreparedStatement pst = con.prepareStatement(query
// bind the parameter with code value
// Obtain the result-set for the selected airline record
ResultSet result = pst.executeQuery();
// get user home folder name
if( result.next() ) {
gifdata = result.getBinaryStream(1);
// create new file
File gifFile = new File(fullName);
// Write the byte array into a local file
FileOutputStream file= new FileOutputStream(gifFile);
int chunk=0;
// write to the local file until image (LONGRAW) data is found
while( (chunk = gifdata.read()) != -1) {
file.write(chunk);
}
// flush the data
file.flush();
file.close(); // close the file
}
}
catch(Exception e) {
out.println("Exception :: "+ e);
}
Its giving me java.lang.NullPointerExcep
could anyone correct me please?
Hii,
where u are populating the value of "fullName"..may be this can be the reason.
Hope this helps
fargo
where u are populating the value of "fullName"..may be this can be the reason.
Hope this helps
fargo
Hii,
Instead óf writing it into file and displaying the file...
U can even use the following servlet to display it directly ...
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;
public class DisplayServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
res.setContentType("image/ gif");
try {
String url = "u need to specify";
String jdbcDriver = "u need to specify";
Class.forName(jdbcDriver);
Connection con = DriverManager.getConnectio n(url);
Statement stmt = con.createStatement();
String query = "SELECT image FROM Images";
ResultSet rs1 = stmt.executeQuery(query);
if (rs1.next()){
ServletOutputStream o= res.getOutputStream();
InputStream in=rs1.getBinaryStream(1);
byte[] bt = new byte [1000];
for(int i = in.read(bt); i != -1; )
{
o.write(bt);
in.read(bt);
}
o.flush();
o.close();
}
con.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
Hope this helps
fargo
Instead óf writing it into file and displaying the file...
U can even use the following servlet to display it directly ...
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;
public class DisplayServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
res.setContentType("image/
try {
String url = "u need to specify";
String jdbcDriver = "u need to specify";
Class.forName(jdbcDriver);
Connection con = DriverManager.getConnectio
Statement stmt = con.createStatement();
String query = "SELECT image FROM Images";
ResultSet rs1 = stmt.executeQuery(query);
if (rs1.next()){
ServletOutputStream o= res.getOutputStream();
InputStream in=rs1.getBinaryStream(1);
byte[] bt = new byte [1000];
for(int i = in.read(bt); i != -1; )
{
o.write(bt);
in.read(bt);
}
o.flush();
o.close();
}
con.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
Hope this helps
fargo
ASKER
could you give me an example of retrieving image of longraw type from database in to a jsp page?
i copied the above code from oracle site. i have no idea of what i am doing. i just have to display a image in a jsp page. i am so badly struck kindlly help me...
thanks a lot
http://otn.oracle.com/sample_code/tech/java/sqlj_jdbc/files/basic/LongRawSample/LongRawSample.java.html
i copied the above code from oracle site. i have no idea of what i am doing. i just have to display a image in a jsp page. i am so badly struck kindlly help me...
thanks a lot
http://otn.oracle.com/sample_code/tech/java/sqlj_jdbc/files/basic/LongRawSample/LongRawSample.java.html
Hello,
Well..what about the fullName variable..u are not doing anything with it...u assigned a null. In the source code..
fullName = userHome+File.separator+na me+".gif";
u need to define the path for the file...
userHome is taken from system property...even u can hardcode it..or use the way u want.
hope this helps
even u can use the servlet for the same.
Hope this helps
fargo
Well..what about the fullName variable..u are not doing anything with it...u assigned a null. In the source code..
fullName = userHome+File.separator+na
u need to define the path for the file...
userHome is taken from system property...even u can hardcode it..or use the way u want.
hope this helps
even u can use the servlet for the same.
Hope this helps
fargo
ASKER
servlets have a problem in my mac. is it possible to write a scriptlet of java code to display the image in my jsp page.. sorry for bothering you. i really appreciate your help.
thanks
thanks
yeah...u can use the code u mentioned directly in ur jsp code...U need to set the content type in jsp. But, u didn't mention about the points i raised in my previous post.
hope this helps
fargo
hope this helps
fargo
Ohh..i m sorry...i think u are creating a gif file and then displaying that...in that case, u need "not" to set the content type to "image/gif".
Fargo
Fargo
ASKER
i get this
Exception :: java.lang.NullPointerExcep tion
<%
InputStream gifdata = null;
String fullName = null;
try{
Class.forName("oracle.jdbc .driver.Or acleDriver ");
DriverManager.registerDriv er(new oracle.jdbc.driver.OracleD river());
Connection con= DriverManager.getConnectio n("<EDITED BY SpideyMod>");
session.putValue("con",con );
Statement stmt1 = con.createStatement();
String query = "select IMAGE FROM IMAGES";
PreparedStatement pst = con.prepareStatement(query );
// bind the parameter with code value
// Obtain the result-set for the selected airline record
ResultSet result = pst.executeQuery();
// get user home folder name
if( result.next() ) {
// Fetch column values
// Obtain the airline code
// append the file name with user home directory,
// file separator and file extension GIF
/* LONGRAW data can be accessed in two ways:
1) By retrieving all the data at once (using getBytes method)
2) By using streams. The LONGRAW data is made available to
the program
as a stream, and the data can be retrieved
chunk by chunk, which is more eficient in terms of memory usage
In this sample we illustrate retrieval using streams method.
*/
gifdata = result.getBinaryStream(1);
// create new file
File gifFile = new File(fullName);
fullName ="/"+"usr"+"/"+"local"+"/" +".gif";
// Write the byte array into a local file
FileOutputStream file= new FileOutputStream(gifFile);
int chunk=0;
// write to the local file until image (LONGRAW) data is found
while( (chunk = gifdata.read()) != -1) {
file.write(chunk);
}
// flush the data
file.flush();
file.close(); // close the file
}
}
catch(Exception e) {
out.println("Exception :: "+ e);
}
%>
Exception :: java.lang.NullPointerExcep
<%
InputStream gifdata = null;
String fullName = null;
try{
Class.forName("oracle.jdbc
DriverManager.registerDriv
Connection con= DriverManager.getConnectio
session.putValue("con",con
Statement stmt1 = con.createStatement();
String query = "select IMAGE FROM IMAGES";
PreparedStatement pst = con.prepareStatement(query
// bind the parameter with code value
// Obtain the result-set for the selected airline record
ResultSet result = pst.executeQuery();
// get user home folder name
if( result.next() ) {
// Fetch column values
// Obtain the airline code
// append the file name with user home directory,
// file separator and file extension GIF
/* LONGRAW data can be accessed in two ways:
1) By retrieving all the data at once (using getBytes method)
2) By using streams. The LONGRAW data is made available to
the program
as a stream, and the data can be retrieved
chunk by chunk, which is more eficient in terms of memory usage
In this sample we illustrate retrieval using streams method.
*/
gifdata = result.getBinaryStream(1);
// create new file
File gifFile = new File(fullName);
fullName ="/"+"usr"+"/"+"local"+"/"
// Write the byte array into a local file
FileOutputStream file= new FileOutputStream(gifFile);
int chunk=0;
// write to the local file until image (LONGRAW) data is found
while( (chunk = gifdata.read()) != -1) {
file.write(chunk);
}
// flush the data
file.flush();
file.close(); // close the file
}
}
catch(Exception e) {
out.println("Exception :: "+ e);
}
%>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I received a message asking for immediate deletion, but I was unsure whether it was the question that desired deletion or the text that I did delete above, so I took the safer of the 2 options. Let me know if this is correct.
SpideyMod
Community Support Moderator @Experts Exchange
SpideyMod
Community Support Moderator @Experts Exchange
Hii,
Well, i don't have any problem. But, i m surprised that the user didn't clarify the reason for deletion.
Regards
fargo
Well, i don't have any problem. But, i m surprised that the user didn't clarify the reason for deletion.
Regards
fargo
Me too. I'm not going to delete the question until I hear back from venu101 anyway. I don't want to throw a possible answer away. Personally, I suspect it was the comment deletions.
SpideyMod
Community Support Moderator @Experts Exchange
SpideyMod
Community Support Moderator @Experts Exchange
Force Accepting as this issue has remained open too long.
SpideyMod
Community Support Moderator @Experts Exchange
SpideyMod
Community Support Moderator @Experts Exchange
ASKER