CREATE TABLE `imageupload` (
`id` bigint(20) NOT NULL auto_increment,
`imagefile` blob NOT NULL,
PRIMARY KEY (`id`)
)
Step:2 To create a web page "image.jsp"
<%@ page import="java.sql.*,java.io.*,java.util.*" %>
<%
String connectionURL = "jdbc:mysql://localhost:3306/userdetails";
if(request.getParameter("imgid")!=null && request.getParameter("imgid")!="")
{
int id = Integer.parseInt(request.getParameter("imgid"));
String filename = "image"+id+".jpg";
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection(connectionURL,"root","root");
Statement st1=con.createStatement();
String strQuery = "select imagefile from imageupload where id="+id;
ResultSet rs1 = st1.executeQuery(strQuery);
String imgLen="";
if(rs1.next()){
imgLen = rs1.getString(1);
}
rs1 = st1.executeQuery(strQuery);
if(rs1.next()){
int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = rs1.getBinaryStream(
1
);
int
index=readImg.read(rb,
0
, len);
st1.close();
response.reset();
response.setContentType(
"image/jpg"
);
response.setHeader(
"Content-disposition"
,
"attachment; filename="
+filename);
response.getOutputStream().write(rb,
0
,len);
response.getOutputStream().flush();
}
}
catch
(Exception e){
e.printStackTrace();
}
}
%>
Step:3 To create a "imageDownload.jsp"
<%
@
page
import
=
"java.sql.*,java.io.*,java.util.*"
%>
Download Images
No comments:
Post a Comment