Thứ Năm, 4 tháng 9, 2014

Uploading images into database server using jsp....

Hi guys,,,,,,

in my last tutorial i shown you how to upload images to server using simple java code.This time i will give you the example to upload images using jsp.

First you need to create a database table to store your image,let the table name be uploadImage

create table uploadImage(id int(500),image longblob);

The above query creates a table in your database.

Now I will write code to upload image using jsp.

let me first create a page that asks the user to browse an image from local machine

content.html
==============
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Content</title>
</head>
<body>

<form  method=""  action="upload.jsp">

<h3>Upload your Image </h3>
<input type="text" name="id" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="file" name="imageData" />

<input type="submit" value="submit" >

</form>
</body>
</html>


now,we have to write code for upload.jsp(see the action method  in form )

uploadjsp.jsp
------------------------
<%@ page language="java" import="java.sql.*,java.io.*,javax.sql.*" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Data Uploading</title>
</head>
<body>
<%!
Connection con=null;
PreparedStatement pst=null;
ResultSet rs=null;
String sql="insert into uploadImage values(?,?)";
File f=null;
FileInputStream fin=null;
%>
<%
try
{
String path="C:/wamp/";
String content=request.getParameter("imageData");
int identifier=Integer.parseInt(request.getParameter("id"));
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/shasheikumar","root","shashei");
pst=con.prepareStatement(sql);
pst.setInt(1,identifier);
f=new File(path+content);
fin=new FileInputStream(f);
pst.setBinaryStream(2,(InputStream)fin,(int)f.length());
int res=pst.executeUpdate();
if(res>0)
{
    response.sendRedirect("success.html");
}
else
{
    response.sendRedirect("failure.html");
}
}
catch(Exception e)
{
    out.print(e.getLocalizedMessage());
}


%>
</body>
</html>


success.html
------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>Your image loaded successfully</h3>

</body>
</html>


failure.html
--------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>Oooops there is an error occured during processing</h3>
</body>
</html>



output
-------------------------------------------

now ,,if the image uploaded successfully you will get


 Finally you can check your uploaded image by moving into your database
type:

select * from uploadImage;





Không có nhận xét nào:

Đăng nhận xét