472,128 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,128 software developers and data experts.

How to retrive image file from MS access database and display this in another JSPpage

How to retrive image file from MS access database and display this in another JSPpage
--------------------------------------------------------------------------------

This is my Jsp code for image upload in database:

-----------Upload.jsp----------------
<html>

<head>
<title>Account Details </title>
</head>
<body bgproperties="fixed" bgcolor="#CCFFFF">
<form method="POST" action="UploadPicture.jsp" enctype="multiform/form-data">

<%! int update=0; %>

<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.sql.Date" %>
<%@ page import="java.io.*"%>

<%@ page language = "java" %>

<%
try
{
String ct="3";
String path;

File image=new File(request.getParameter("upload"));
path=request.getParameter("upload");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:itPlusElect ronics","","");

PreparedStatement pstmt=con.prepareStatement("insert into graphics values(?,?,?)");
pstmt.setString(2,path);
pstmt.setString(3,ct);

InputStream is=new FileInputStream(path);
pstmt.setBinaryStream(1, is, (int)(image.length()));
int s=pstmt.executeUpdate();
if(s>0)
{
out.println("Uploaded");
}
else
{
%>
unsucessfull
<%}
is.close();
pstmt.close();
}
catch(Exception e)
{
}%>
</p>

<p><br>
<img src="UploadedPicture.jsp">image</img>

<p></p>
</form>
</body>
</html>

-------------------------------------------------------------
My database name is itPlusElectronics and the table name is "graphics".
I have seen as a result of the above code that the image is stored in database as "Long binary data". and database table is look like as follows-------
picture path id
Long binary data D:\AMRIT\1-1-Picture.jpg 3

------------------------------------------------------------------------------
To retrive and display i use this JSP code as--

------------------------UploadedPicture.jsp------------------------------

<html>

<head>
<title>Account Details </title>
</head>
<body bgproperties="fixed" bgcolor="#CCFFFF">

<%! int update=0; %>

<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.text.*" %>

<%@ page import="java.io.*"%>
<%@ page language = "java" %>
<%@page import="javax.servlet.ServletOutputStream"%>

<%

try
{


String path;
path=request.getParameter("upload1");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:itPlusElect ronics","","");

PreparedStatement pst = con.prepareStatement("SELECT * FROM graphics WHERE id ='3'");
// pst.setString(3, id);
ResultSet rs = pst.executeQuery();
path=rs.getString("path");
if(rs.next()) {


byte[] bytearray = new byte[4096];
int size=0;
InputStream sImage;
sImage = rs.getBinaryStream(1);
response.reset();
response.setContentType("image/jpeg");
response.addHeader("Content-Disposition","filename=path");
while((size=sImage.read(bytearray))!= -1 )
{
response.getOutputStream().write(bytearray,0,size) ;
}
response.flushBuffer();
sImage.close();
rs.close();
}

}
catch(Exception e)
{
}
%>


</body>
</html>

------------------------------------------------------------------------------
Now after browsing a jpg image file from client side and pressing submit button ;
I am unable display the image in the Upload.jsp file.Though I use
<img src="UploadedPicture.jsp">image</img> HTML code in Upload.jsp
file .

Now I am unable to find out the mistakes which is needed for displaying the picture in the Upload.jsp page..
If any one can help with the proper jsp code to retrive and display the image ,please please help me !!!!!!!!!!!!!!!!!!!!!!!!!!
Dec 29 '07 #1
1 2769
ajos
283 100+
How to retrive image file from MS access database and display this in another JSPpage
--------------------------------------------------------------------------------

This is my Jsp code for image upload in database:

-----------Upload.jsp----------------
<html>

<head>
<title>Account Details </title>
</head>
<body bgproperties="fixed" bgcolor="#CCFFFF">
<form method="POST" action="UploadPicture.jsp" enctype="multiform/form-data">

<%! int update=0; %>

<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.sql.Date" %>
<%@ page import="java.io.*"%>

<%@ page language = "java" %>

<%
try
{
String ct="3";
String path;

File image=new File(request.getParameter("upload"));
path=request.getParameter("upload");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:itPlusElect ronics","","");

PreparedStatement pstmt=con.prepareStatement("insert into graphics values(?,?,?)");
pstmt.setString(2,path);
pstmt.setString(3,ct);

InputStream is=new FileInputStream(path);
pstmt.setBinaryStream(1, is, (int)(image.length()));
int s=pstmt.executeUpdate();
if(s>0)
{
out.println("Uploaded");
}
else
{
%>
unsucessfull
<%}
is.close();
pstmt.close();
}
catch(Exception e)
{
}%>
</p>

<p><br>
<img src="UploadedPicture.jsp">image</img>

<p></p>
</form>
</body>
</html>

-------------------------------------------------------------
My database name is itPlusElectronics and the table name is "graphics".
I have seen as a result of the above code that the image is stored in database as "Long binary data". and database table is look like as follows-------
picture path id
Long binary data D:\AMRIT\1-1-Picture.jpg 3

------------------------------------------------------------------------------
To retrive and display i use this JSP code as--

------------------------UploadedPicture.jsp------------------------------

<html>

<head>
<title>Account Details </title>
</head>
<body bgproperties="fixed" bgcolor="#CCFFFF">

<%! int update=0; %>

<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.text.*" %>

<%@ page import="java.io.*"%>
<%@ page language = "java" %>
<%@page import="javax.servlet.ServletOutputStream"%>

<%

try
{


String path;
path=request.getParameter("upload1");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:itPlusElect ronics","","");

PreparedStatement pst = con.prepareStatement("SELECT * FROM graphics WHERE id ='3'");
// pst.setString(3, id);
ResultSet rs = pst.executeQuery();
path=rs.getString("path");
if(rs.next()) {


byte[] bytearray = new byte[4096];
int size=0;
InputStream sImage;
sImage = rs.getBinaryStream(1);
response.reset();
response.setContentType("image/jpeg");
response.addHeader("Content-Disposition","filename=path");
while((size=sImage.read(bytearray))!= -1 )
{
response.getOutputStream().write(bytearray,0,size) ;
}
response.flushBuffer();
sImage.close();
rs.close();
}

}
catch(Exception e)
{
}
%>


</body>
</html>

------------------------------------------------------------------------------
Now after browsing a jpg image file from client side and pressing submit button ;
I am unable display the image in the Upload.jsp file.Though I use
<img src="UploadedPicture.jsp">image</img> HTML code in Upload.jsp
file .

Now I am unable to find out the mistakes which is needed for displaying the picture in the Upload.jsp page..
If any one can help with the proper jsp code to retrive and display the image ,please please help me !!!!!!!!!!!!!!!!!!!!!!!!!!
Hi,
how about this link here image servlet . Im sorry i havent seen your problem exactly. But the link here serves a image from a path outside of the web container.
Haaaapy New Year :)
regards,
ajos
Dec 31 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Eric Keung | last post: by
2 posts views Thread by Wayne Wengert | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.