Dear all,
I was stored the gif/jpg image into the MySQL Database as a buffered image.
But when i was retrive the image from database and display in jsp page, it will not working well.
The image is not displaying.
The content get from the database, but it's type is mismatch i think.
Here are the code i was using.
FOR UPLOADING IMAGE
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page language="java" %>
<%@ page import="java.awt.image.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.File"%>
<%@ page import="javax.imageio.ImageIO"%>
<%@ page import="java.awt.image.BufferedImage,java.util.*"%>
<%@ page import="java.awt.*"%>
<%@ page import="java.util.*,com.oreilly.servlet.MultipartRequest"%>
<%
/* The Following Code is Used To Insert An Image Into Database */
String filename="";
try
{
//Download com.oreilly package
MultipartRequest multi= new MultipartRequest(request,".",5*1024*1024);
Enumeration files=multi.getFileNames();
File f=null;
while(files.hasMoreElements())
{
String name=(String)files.nextElement();
filename=multi.getFilesystemName(name);
String type=multi.getContentType(name);
f=multi.getFile(name);
System.out.println("The File is "+f);
}
Connection con=null;
String userName="root";
String password = "veradis";
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample",userName,password);
Statement stmt = con.createStatement();
InputStream is = new FileInputStream(f);
byte b[]=new byte[is.available()];
is.read(b);
int flag=0;
try
{
String sql = "INSERT into tbl_image(image) values('" + b + "')";
System.out.println(sql);
stmt.execute(sql);
flag=1;
}
catch(Exception e)
{
System.out.println("SQL Exception : " + e);
}
if(flag==1)
{
System.out.println("Query Executed Successfully");
}
stmt.close();
}
catch(Exception e)
{
System.out.println(e);
}
out.println("The Image is Added into Database");
%>
and FOR RETRIVE AND DISPLAY FROM THE DATABASE
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page language="java" %>
<%@ page import="java.awt.image.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="java.io.File"%>
<%@ page import="javax.imageio.ImageIO"%>
<%@ page import="java.awt.image.BufferedImage,java.util.*"%>
<%@ page import="java.awt.*"%>
<html>
<head><title>JSP Page</title></head>
<body>
<%
try
{
javax.servlet.http.HttpServletResponse res=null;;
int returnValue = 0;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
InputStream in = null;
OutputStream os = null;
Blob blob = null;
String text;
text=request.getParameter("text");
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","root","veradis");
final String query = "SELECT image FROM tbl_image";
conn.setAutoCommit(false);
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
int i=1;
if(rs.next())
{
String len1 = rs.getString("image");
int len = len1.length();
byte [] b = new byte[len];
in = rs.getBinaryStream("image");
int index = in.read(b, 0, len);
OutputStream outImej = new FileOutputStream("C:/Documents and Settings/Tamil/Desktop/photo/img"+i+".JPG");
while (index != -1)
{
outImej.write(b, 0, index);
index = in.read(b, 0, len);
System.out.println("==========================");
System.out.println(index);
System.out.println(outImej);
System.out.println("==========================");
}
outImej.close();
i++;
}
else
{
returnValue = 1;
}
}
catch(Exception e)
{
out.println("SQLEXCEPTION : " +e);
}
%>
</body>
</html>
If i can run this code the image column was displayed as empty.
If anyone can know how to solve this problem, please help me.
Regards
Tamilvanan