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

Ads
 

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

1)page.jsp:

<HTML>
<FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<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>
<tr><td colspan="2" align="center">&nbsp;</td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Send File"> </td></tr>
<table>
</center>
</FORM>
</HTML>

2)upload.jsp:

<%@ page import="java.io.*,java.sql.*,java.util.zip.*" %>
<%
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 = in.read(dataBytes, 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));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully upload the file:</b>
<%out.println(saveFile);%></td></tr></table>
<%
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/test";
PreparedStatement psmnt = null;
FileInputStream fis;
InputStream sImage;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
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();
if(s>0){
System.out.println("Uploaded successfully !");
Statement st=connection.createStatement();
      ResultSet rs=st.executeQuery("SELECT images FROM image");
      if(rs.last()){
      byte[] bytearray = new byte[1048576];
      int size=0;
      sImage = rs.getBinaryStream(1);
      response.reset();
      response.setContentType("image/jpeg");
      while((size=sImage.read(bytearray))!= -1 ){
      response.getOutputStream().write(bytearray,0,size);
   }
}
}
else{
System.out.println("Error!");
}
}
catch(Exception e){e.printStackTrace();}
}
%>

Ads









Related Tutorials/Questions & Answers:
How save,get picture from database in my jsp page?
How to save and get value from JSP
Advertisements
we make a picture as a background on my web pages
to get picture from ms access database by jsp-servlet....
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 store and retrieve image from database in JSP?
how to display data from database in jsp
how gave security of my pages in website????
How to retrieve image from mysql database in JSP?
How to capture picture from webcam using java
Java to insert picture to database - JSP-Servlet
jsp or sevlet and html form to send picture to database - JSP-Servlet
how to get data from database into dropdownlist in jsp
How can I to my database to my application
How to show data from database in textbox in jsp
how to generate reports from oracle database using jsp and ajax code
how to retrieve images from database to jsp?
how to get data from database into dropdownlist in jsp
how to get data from database into dropdownlist in jsp
how to display data from jsp file into database
How to retrieve image from database using jsp and servlet?
connect to the database from JSP
How to display image in jsp from database using Servlet?
How to connect to the database to my application if my database is made up in oracle
How to get the data from the database using Servlet or JSP program
how to display values from database into table using jsp
Get values from JSP pages - JSP-Servlet
how to add data dynamically from database into a dropdown list in a jsp

Ads