Mon Jun 29, 2009 6:12 pm
package sg.nyp.edu.sit;
import java.sql.*;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DisplayImage
*/
public class DisplayImage extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public DisplayImage() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String connectionURL = "jdbc:mysql://localhost/images";
java.sql.Connection con= null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(connectionURL,"root","");
Statement st1=con.createStatement();
ResultSet rs1 = st1.executeQuery("select image from images where imageID ='2'");
String imglen="";
if(rs1.next()){
imglen = rs1.getString(1);
System.out.println(imglen.length());
}
rs1 = st1.executeQuery("select image from images where imageID = '2'");
if (rs1.next()){
int len = imglen.length();
byte[] rb = new byte [len];
InputStream readImg = rs1.getBinaryStream(1);
int index=readImg.read(rb,0,len);
System.out.println("index" + index);
st1.close();
response.reset();
response.setContentType("image/jpg");
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
}
}
catch (Exception e){
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
package sg.nyp.edu.sit.model;
public class Image {
int imageID;
Byte image;
String imglen;
public int getImageID() {
return imageID;
}
public void setImageID(int imageID) {
this.imageID = imageID;
}
public Byte getImage() {
return image;
}
public void setImage(Byte image) {
this.image = image;
}
public String getImglen() {
return imglen;
}
public void setImglen(String imgLen) {
this.imglen = imgLen;
}
}
Mon Jun 29, 2009 10:00 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.