You are on page 1of 8

Store Images in Database

1. Create Table in Database userinformation which contains 6 columns mentioned as below.


a. First name – varchar
b. Last name – varchar
c. Email -varchar
d. Password – varchar
e. Images – blob
2. Create JSP file Register

3. Create servlet class Registration


4. Create jsp file view jsp

5. Create servlet class viewimage


Explanation:

Working with Large Objects

 enctype(ENCode TYPE) attribute specifies how the form-data


should be translated while taking in a server.
 multipart/form-data is one of the value of enctype attribute, which
is used in form element to specify regarding a file upload
operation.

Syntax -
<form action="FileStorage" enctype="multipart/form-data" method =
"post">

 The "@MultipartConfig annotation" is used to annotate a servlet


class in order to handle multiple types of data which taken from
html form like normal data and upload files as well as it will
configure various upload file settings.
 With above annotation programmers are making use of
"maxfilesize" attribute to specify maximum size allowed in
servlet class.
 Part is a interface which is present in javax.servlet package which
is used to store file part of html form and to get that part we have
to make use of "getPart(identifier)".

Syntax -
Part filepart = request.getPart("value");

 To convert part of html form i.e. file into byte, make use
of "getInputStream()" present in Part interface.
 The return of InputStream class is the superclass of all the
io classes. It represents input stream of bytes.

Syntax -
InputStream is = filepart.getInputStream();

 To store it into database make use setBlob(position of


placeholder, rv of InputStream class)
Retrieve Image from Database.
 To retrieve image from database make use of
"getBlob("column_name")" method which is having return type as
"Blob" interface present in java.sql package.
 To represent binary large object into bytes format, we have to use
"getBytes()" method which is having return types as "byte []"
and present in Blob interface.
 getBytes() will take two arguments.
a. Position of first bytes which is always "1".
b. Length of image i.e. no. of bytes present in image.
 To get length of image make use of length() present in
Blob interface with return type "long".

Note: Array size accepts 'int value' but length() will return long
value. Hence, perform narrowing to convert long into int.
 Syntax-

byte [] arr = blob.getBytes(1,(int) blob.length());

OutputStream
1. To display image on browser make use of OutputStream Class
present in java.io package.
2. Helper method for it is "getOutputStream()" which can access
by using response reference variable.
3. OutputStream class consist following three methods.
a. write(byte [] a) - to store all bytes of image in output stream.
b. flush() - to print all bytes in the form image on browser.
c. close() - to close connection between servlet and browser.
 The getOutputStream() method of
Java Socket class returns an
output stream for the given
socket. If you close the returned
OutputStream then it will close the
linked socket.
 A socket is one endpoint of a two-
way communication link between
two programs running on the
network. A socket is bound to a
port number so that the TCP layer
can identify the application that data
is destined to be sent to. An
endpoint is a combination of an IP
address and a port number.
 Large objects ---> blob(binary large
object ex. Images, audio, files and
video files) and clob(Character
large object text files, rich text files,
etc.)

You might also like