How save,get picture from database in my jsp page?

How save,get picture from database in my jsp page?

How i save picture in db after browsing it,and also how i get it on my other jsp page?????????

View Answers

May 27, 2011 at 10:23 AM


<FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<table border="0" bgcolor=#ccFDDEE>
<center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
<tr><td colspan="2" align="center">&nbsp;</td></tr>
<tr><td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
<tr><td colspan="2" align="center">&nbsp;</td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Send File"> </td></tr>


<%@ page import="*,java.sql.*,*" %>
String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead =, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
%><Br><table border="2"><tr><td><b>You have successfully upload the file:</b>
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/test";
PreparedStatement psmnt = null;
FileInputStream fis;
InputStream sImage;
connection = DriverManager.getConnection(connectionURL, "root", "root");
File f = new File(saveFile);
psmnt = connection.prepareStatement("insert into image(images) values(?)");
fis = new FileInputStream(f);
psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length()));
int s = psmnt.executeUpdate();
System.out.println("Uploaded successfully !");
Statement st=connection.createStatement();
      ResultSet rs=st.executeQuery("SELECT images FROM image");
      byte[] bytearray = new byte[1048576];
      int size=0;
      sImage = rs.getBinaryStream(1);
      while((!= -1 ){
catch(Exception e){e.printStackTrace();}

Related Tutorials/Questions & Answers:
How save,get picture from database in my jsp page?
How to save and get value from JSP
to get picture from ms access database by jsp-servlet....
we make a picture as a background on my web pages
how to retrive data grom database in jsp pages.
How to Open Picture From M.S. Access Database using Java Servlet ?
How to access the database from JSP?
How to retrieve blob image from database in JSP?
how to display data from database in jsp
How to store and retrieve image from database in JSP?
How to retrieve image from mysql database in JSP?
Java to insert picture to database - JSP-Servlet
jsp or sevlet and html form to send picture to database - JSP-Servlet
connect to the database from JSP
How to capture picture from webcam using java
how to get data from database into dropdownlist in jsp
How to show data from database in textbox in jsp
Get values from JSP pages - JSP-Servlet
how to retrieve images from database to jsp?
how gave security of my pages in website????
how to display data from jsp file into database
how to get data from database into dropdownlist in jsp
how to get data from database into dropdownlist in jsp
How to retrieve image from database using jsp and servlet?
How to display image in jsp from database using Servlet?
how to generate reports from oracle database using jsp and ajax code
line chart from database in jsp
how to display values from database into table using jsp
How to get the data from the database using Servlet or JSP program
how to display image and text in single jsp page from the mysql database
Loading a jsp page (with record from database) from another jsp page
how to show effect (visual) on jsp page using value from database
How we delete a data of database from front end jsp page
How to fetch entries/values from database to a jsp page one by one?
Update Database from jsp
how to add data dynamically from database into a dropdown list in a jsp
how to read values from excel sheet and compare with database using jsp
how to store the data in a array retrived from the database - JSP-Servlet
How to get data from Oracle database using JSP
jsp pages for dispatchaction for adding user to database
How retreive data from database without using post method in jsp - JSP-Servlet
How can I to my database to my application
how to upload an image from a jsp page to a mysql database table using jsp
jsp pages
how to check username & password from database using jsp
Connecting to Database from a hyperlink in JSP - JSP-Servlet
How to Retrieve data from database in jsp
How to edit values in textboxes from database using jsp
How to Autogenerate of ID from database and show on JSP page?
How to Autogenerate of ID from database and show on JSP page?