This Post explains how to store a picture in an Oracle 10G database. Then It also explains how to retrieve the picture and display it in a web page.The Image is stored in a BLOB.
create table Pictures(PictureName varchar(100) primary key,Picture Blob)
Then download the org.apache.commons.fileupload, and the org.apache.commons.io packages from the Apache website. I bundled it into a jar file, called it org.jar and added it to the lib directory under Tomcat 6.0
Then develop a website and call it PicturesSite, and copy it to the webapps directory in Tomcat 6.0
Then create a resource entry in Context.xml file under the conf directory.
context.xml
<Resource name="Pictures" Auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:XE"
username="system" password="hypatia"
MaxActive="20" MaxIdle="10" MaxWait="-1"/>
Of course the ojdbc14.jar file (It contains the OracleDriver) has to be copied into Tomcat6.0\lib
Here is the code for the PictureUploaderForm. It will upload two pictures at one go.
PicUpload.html
<html>
<body>
<title>FILE UPLOADING</title>
<form id="f1" enctype="multipart/form-data" method="post" action="uploaderservlet">
PICTURE:<input type="FILE" id="picture" name="picture" />
PICTURE:<input type="FILE" id="pictureWa" name="pictureWa" />
<br/>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>
uploaderservlet.java
package servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.fileupload.disk.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.util.*;
public class uploaderservlet extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
try
{
Context initcontext=new InitialContext();
Context envcontext=(Context)initcontext.lookup("java:/comp/env");
DataSource ds=(DataSource)envcontext.lookup("Pictures");
Connection con=ds.getConnection();
response.getWriter().println(con);
PreparedStatement smt=con.prepareStatement("insert into pictures values(?,?)");
/**********************************/
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(100000000);
fu.setSizeThreshold(4096);
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
while(i.hasNext())
{
FileItem fi = (FileItem)i.next();
String filename = fi.getName();
if(filename!=null)
{
response.getWriter().println(filename);
int last=filename.lastIndexOf("\\");
filename=filename.substring(last+1).trim();
String originalfilename=filename;
filename="webapps\\PicturesSite\\images\\" +filename;
File f=new java.io.File(filename);
fi.write(f);
fi.getOutputStream().flush();
fi.getOutputStream().close();
response.getWriter().println("File uploaded successfully" );
smt.setString(1,originalfilename);
FileInputStream fs=new FileInputStream(f);
smt.setBinaryStream(2,fs,(int)f.length());
smt.executeUpdate();
fs.close();
f=null;
}
}
con.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
Here is the database after the file uploading is done.
To retrieve the Picture I have used a servlet and written the data to the OutputStream of the response. In a subsequent post I'll explain how to do it via a Java Bean, and a Custom Tag.
Here is the Servlet
showpicture.java
package servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
public class showpicture extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
try
{
String name=request.getParameter("picturename");
Context initcontext=new InitialContext();
Context envcontext=(Context)initcontext.lookup("java:/comp/env");
DataSource ds=(DataSource)envcontext.lookup("Pictures");
Connection con=ds.getConnection();
PreparedStatement ps=con.prepareStatement("select picture from pictures where picturename=?");
ps.setString(1,name);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
Blob b=rs.getBlob(1);
OutputStream out=response.getOutputStream();
InputStream in=b.getBinaryStream();
int n=in.read();
while(n!=-1)
{
out.write(n);
n=in.read();
}
out.flush();
out.close();
in.close();
con.close();
}
}
catch(Exception ex)
{
}
}
}
To display the Pictures in an HTML page use the img tag, and send the PictureName via a GET name value pair.
Here is the HTML Page
Pictures.html
<img src="showpicture?picturename=Champak.jpg" alt="Champak.jpg" width="300" height="300"/>
<img src="showpicture?picturename=design.jpg" alt="design.jpg" width="300" height="300"/>
web.xml
<web-app>
<servlet>
<servlet-name>MYFILE</servlet-name>
<servlet-class>servlets.uploaderservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MYFILE</servlet-name>
<url-pattern>/uploaderservlet/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>showimages</servlet-name>
<servlet-class>servlets.showpicture</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>showimages</servlet-name>
<url-pattern>/showpicture/*</url-pattern>
</servlet-mapping>
</web-app>
The given servlet code uses datasource but i'm using type 4 jdbc driver.What change in code and other steps will be required?
ReplyDeleteThanks in Anticipation
sir i m getting this exception
ReplyDeletejavax.naming.NameNotFoundException: Name [Pictures] is not bound in this Context. Unable to find [Pictures].
javax.naming.NameNotFoundException: Name [Pictures] is not bound in this Context. Unable to find [Pictures].
how 2 solve this exception??i hv made changes in context file of apache