日期:2014-05-17 浏览次数:20668 次
public class DBResult {
public static InputStream outImage(Connection conn,String picid) {
int id=Integer.parseInt(picid);
String sql="select pic from p where picid="+id;
PreparedStatement pstmt = null;
ResultSet rs=null;
InputStream is = null;
try{
pstmt = (PreparedStatement) conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){
is = rs.getBlob("pic").getBinaryStream();
}
is.close();
rs.close();
pstmt.close();
}catch (Exception e) {
e.printStackTrace();// TODO: handle exception
}
return is;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf8");
response.setCharacterEncoding("utf8");
String page=null;
String picid = request.getParameter("txtpicid");
if(picid!=null){
response.setContentType("image/jpeg");
Connection conn = dataBase.getConnection();
InputStream is=DBResult.outImage(conn, picid);
if(is!=null){
try{
is=new BufferedInputStream(is);
BufferedImage bi=ImageIO.read(is);
OutputStream os=response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
encoder.encode(bi);
is.close();
os.close();
}catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
request.setAttribute("picid", picid);
page="imageout.jsp";
response.sendRedirect(page);
}