<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.zip.*"%>
<%
String rollno1=request.getParameter("rollno");
String name1=request.getParameter("name");
String age1=request.getParameter("age");
out.println(rollno1);
out.println(name1);
out.println(age1);
%>
<%
//to get the content type information from JSP Request Header
String contentType = request.getContentType();
//here we are checking the content type is not equal to Null and
//as well as the passed data from mulitpart/form-data is greater than or
//equal to 0
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
//we are taking the length of Content type data
int formDataLength = request.getContentLength();
String rollno2=new String(rollno1);
String name2=new String(name1);
String age2=new String(age1);
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//this loop converting the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,
formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
//for saving the file name
String 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;
//extracting the index of file
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;
// creating a new file with the same name and writing the content in new file
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br>
Connection connection = null;
//String connectionURL = "jdbc:mysql://localhost:8080/test";
ResultSet rs = null;
PreparedStatement pst = null;
FileInputStream fis;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
//connection = DriverManager.getConnection(connectionURL, "root", "root");
Connection con = DriverManager.getConnection("jdbc:mysql:///test","root","123");
File f = new File(saveFile);
String query="insert into student values(?,?,?,?)";
pst= con.prepareStatement(query);
//psmnt = connection.prepareStatement("insert into file(file_data) values(?)");
fis = new FileInputStream(f);
pst.setString(1,rollno2);
pst.setString(2,name2);
pst.setString(3,age2);
pst.setBinaryStream(4, (InputStream)fis, (int)(f.length()));
int s = pst.executeUpdate();
if(s>0){
out.println("Uploaded successfully !");
}
else{
out.println("Error!");
}
}
catch(Exception e){e.printStackTrace();}
}
%>
<% // declare a connection by using Connection interface Connection connection = null;
/* Create string of connection url within specified format with machine
name, port number and database name. Here machine name id localhost
and database name is mahendra. */
//String connectionURL = "jdbc:mysql://localhost:3306/mahendra";
/*declare a resultSet that works as a table resulted by execute a specified
sql query. */
ResultSet rs = null;
// Declare statement.
PreparedStatement psmnt = null;
Connection connection=null;
// declare InputStream object to store binary stream of given image.
InputStream sImage;
try {
HttpSession h1=request.getSession();
String rollno=(String)h1.getAttribute("rollno");
// Load JDBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes
parameters of string type connection url, user name and password to
connect to database. */
connection = DriverManager.getConnection("jdbc:mysql:///test","root","123");
/* prepareStatement() is used for create statement object that is
used for sending sql statements to the specified database. */
psmnt = connection.prepareStatement("SELECT file_data FROM student WHERE rollno = ?");
psmnt.setString(1,rollno); // here integer number '11' is image id from the table
rs = psmnt.executeQuery();
if(rs.next()) {
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);
}
}
}
catch(Exception ex){
out.println("error :"+ex);
}
finally {
// close all the connections.
rs.close();
psmnt.close();
connection.close();
}
%>
Related Tutorials/Questions & Answers:
Advertisements
Saving image and text in mysql db using spring? Saving
image and text in
mysql db using spring? Hii,
My requirement is to save the employee details(including
image) into
mysql db.i want to save the
image location in
db .And actual
image(binary formate) should save
Image upload in mysql database using jsp servletImage upload in
mysql database
using jsp servlet Hello, I need code to insert
image in
mysql database, I have seen the code which is already in your... different format of file to upload into
mysql db like pdf and doc file
Retrieving data from data base using jsp combo boxRetrieving data from
data base
using jsp combo box Hi guys please help me , i have on GUI page int that Server type(like apache,jboss,weblogic) one combo box is there and another filed is version of the server(like 1.0,2.0) like
upload csv to mysql db using jsp uploadupload csv to
mysql db using jsp upload Hello all,
Please give me the code to uplad .csv from
jsp page and insert values into
MySQl db. I have a table with 8 cloumns.(MDN--varchar(30),Otafdate date,crt varchar(30),dmdn
retrieving info from DB using struts?retrieving info from
DB using struts? Hi. I was looking info about
retrieving info from a database
using struts.
I need a .java that I suppose connects to a database and show the info on a
jsp.
Somebody could help me
Insert Blob(Image) in Mysql table using JSPInsert Blob(
Image) in
Mysql table
using JSP
In this Section, we will insert blob
data(
image) in
Mysql database table
using JSP code.
A Blob stores a binary..._TO_REPLACE_8
See also :
Display Blob(
Image) in
Mysql table
using retrieving from db - JSP-Servletretrieving from db hello'
I am trying to write my first application...: Exception in
JSP: /jsp1/mydb.
jsp:25
22: if(request.getParameter("action...; Hi
Retrive value from database
Retrive
data from
retrieve related data from database using jsp and mysqlretrieve related
data from database
using jsp and mysql Hi sir,
please give some example of
jsp code for
retrieving mysql database values in multiple dropdown list. if we change a value in a dropdown its related value must
How to retrieve image from mysql database in JSP?How to retrieve
image from
mysql database in
JSP? Hi,
I need
JSP... to retrieve
image from
mysql database in
JSP?
Thanks
Hi,
You can write SQL query to find the
data from database and get the
image from resultset
using Exporting data from mysql into csv using jspExporting
data from
mysql into csv
using jsp Hi friends.... I want to export the
data from
mysql to csv file
using... i am having 30 columns in my database.. Eg- text1,text2,text3,....,upto text30... i want to export this
data Storing Multiple image in sql using javaStoring Multiple
image in sql
using java Hi,
How to store and retrieve a multiple
image in sql
using java but already i have created sql table if i want to insert a
image while runtime execution.
Can anyone tell me solution
Display Blob(Image) from Mysql table using JSP Display Blob(
Image) from
Mysql table
using JSP
In this section, we will display blob
data(
image) from
Mysql database table
using JSP code.
A Blob stores... Also :
Insert Blob(
Image) in
Mysql table
using JSP
Download Source CodeADS
Image retrieval from mysql - JSP-ServletImage retrieval from mysql Hai friends,
I used the following code to insert
image into
mysql db.
Db data type is blob....
Inserted file
image does not exist database limit. But, while
retrieving image Retrieving XML Data Using GWT Retrieving XML
Data Using GWT
This Example Describes the way of
retrieving XML file
Data from the server
using GWT. The basic building
block for running this example which we
JSP:select image for db and display in image tagJSP:select
image for
db and display in
image tag Hi, i am new to this forum.
My query is that, i am trying to display
image in
image tag but i am not getting, how to do this by
using this code. plz help me.
if(rs1.next
JSP:select image for db and display in image tagJSP:select
image for
db and display in
image tag Hi, i am new to this forum.
My query is that, i am trying to display
image in tag but i am not getting, how to do this by
using this code. plz help me.
if(rs1.next
JSP:select image for db and display in image tagJSP:select
image for
db and display in
image tag Hi, i am new to this forum.
My query is that, i am trying to display
image in tag but i am not getting, how to do this by
using this code. plz help me.
if(rs1.next
upload image using JSP Hibernateupload
image using JSP Hibernate sir,
I want to take
image from user and save to database(
MYSQL)
using Hibernate and
JSP
Thanks in advance
display image using jspdisplay
image using jsp display
image using jsp and phonegap on emulator of eclipse
Here is a simple
jsp code that displays an
image on browser.
<%@ page import="java.io.*" %>
<%@page contentType="
image/gif
Image in mysqlImage in mysql Hi. How to insert and retrieve images in
mysql db using JSP or JAVA Servlet? Thanks in advance
Uploading image using jspUploading
image using jsp how to upload
image using jsp. Already i... that
image file ...
I want know that solution
using by u...
Thanks,
P.S.N.
Here is a
jsp code that upload
image and display it on the browser.
1