Please Help me Retrive the Image from MySQL Database

I have upload the Image into MySQL Database Table using BLOB Datatype. It's working successfully. I have upload the image into Database as a Binary Data.

But the problem is to retrive and display the image from Databse.

I need to retrive the image, which is store in the Database table, and display it on jsp page. I have upload the image using jsp page also.

I have using the following code for Upload the image.

<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ 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"%>



Sample Image Upload



<%@ include file="../top.jsp"%>

<%
/* The Following Code is Used To Insert An Image Into Database */
String filename="";
int flag=0;
try
{
//Download com.oreilly package

MultipartRequest multi= new MultipartRequest(request,".",5*1024*1024);
Enumeration files=multi.getFileNames();
File f=null;
String name="";
while(files.hasMoreElements())
{
name=(String)files.nextElement();
filename=multi.getFilesystemName(name);
String type=multi.getContentType(name);
f=multi.getFile(name);
String userName="root";
String password = "veradis";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample", "root", "veradis");
String INSERT_PICTURE = "insert into tbl_image(image,name) values (?,?)";
FileInputStream fis = null;
PreparedStatement ps = null;

String fname = filename.replaceAll(" ","_");
String appPath = f.getPath();
fis = new FileInputStream(f);
ps = conn.prepareStatement(INSERT_PICTURE);
ps.setBinaryStream(1, fis, (int) f.length());
ps.setString(2,fname);
ps.executeUpdate();
flag=1;
}

if(flag==1)
{
System.out.println("Insert Query Executed Successfully");
}
}
catch(Exception e)
{
flag=2;
out.println(e);
}

if(flag !=2)
{
out.println("The Image is Added into Database");
}
%>





Please help me with Retrive the image from Database(Binary Image Data). If anybody has the code or sample example for Retriving Binary image from MySQL Database please send me

Thanks in advance

Thanks

Tamilvanan
ttamilvanan81@gmail.com
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories