通过Java向JSP页面生成图片
2013-01-17 16:07
393 查看
通过Java向JSP页面生成图片
页面上显示图片的方法
<img src="<%=request.getContextPath()%>/ShowPic?pic_id=???此处是图片在数据库中存储的ID??? />
public class ShowImage extends HttpServlet { private static final Logger log = Logger.getLogger(ShowImage.class); public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { Connection con=null; ResultSet rs=null; Statement st=null; try { ApplicationContext ctx =WebApplicationContextUtils.getWebApplicationContext(this.getServletContext()); DataSource ds = (DataSource) ctx.getBean("dataSource"); con = ds.getConnection(); st= con.createStatement(); String sql = ""; if(null!=req.getParameter("tableName")&&req.getParameter("tableName").equals("ecc_oc.tuan_pic")){ sql ="select PIC_ID,PIC_ID,DATA from ecc_oc.tuan_pic where PIC_ID="+ Integer.parseInt((req.getParameter("pic_id")==null)?"-1":req.getParameter("pic_id")); }//当是图案代码 要显示图形而来 else if(null != req.getParameter("fromTuan") && !req.getParameter("fromTuan").equals("") && req.getParameter("fromTuan").equals("design")){ sql = "select PIC_ID,DATA_SOURCE,DATA from ecc_oc.tuan_pic where PIC_ID = "+ Integer.parseInt(req.getParameter("pic_id")==null?"0":req.getParameter("pic_id")); } else { sql ="select pic_data_id, pic_id, data from ecc_oc.picture_data where pic_id="+ Integer.parseInt(req.getParameter("pic_id")); } // System.err.println("show img: " + sql); rs= st.executeQuery(sql); Blob bl = null; InputStream ins = null; while (rs.next()) { bl = rs.getBlob(3); ins = bl.getBinaryStream(); } if (ins != null) { OutputStream out = res.getOutputStream(); byte buffer[] = new byte[512]; int len = 0; while ((len = ins.read(buffer)) != -1) { out.write(buffer, 0, len); } ins.close(); out.close(); } res.setContentType("image/*;charset=GB2312"); rs.close(); st.close(); con.close(); } catch (Exception ex) { log.error("",ex); }finally { try { if(rs!=null)rs.close(); if(st!=null)st.close(); if(!con.isClosed())con.close(); } catch (SQLException e) { log.error("",e); } } } public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { doGet(req, res); } }
页面上显示图片的方法
<img src="<%=request.getContextPath()%>/ShowPic?pic_id=???此处是图片在数据库中存储的ID??? />
相关文章推荐
- java 通过流的方式读取本地图片并显示在jsp 页面上(类型以jpg、png等结尾的图片)
- java 通过流的方式读取远程图片并显示在jsp页面(类型以jpg、png等结尾的图片)
- java 通过流的方式读取本地图片并显示在jsp 页面上(类型以jpg、png等结尾的图片)
- jsp页面获取java生成的图片验证码
- Java通过IO流读取图片并显示在JSP页面上面
- java通过commons-fileupload实现多张图片的上传(jsp页面)
- JSP实现页面验证功能随机生成图片
- java中根据图片的url将文件回显在jsp页面代码
- 在java中生成二维码,并直接输出到jsp页面
- java将文档转换成html页面代码 (doc中的图片生成资源文件)
- jsp中插入java代码,并在当前页面获取值(也可在另一js种通过input标签id获取值)
- weblogic容器通过JSP页面输出图片出现乱码Tomcat下却正常
- 将图片转成base64字符串并在JSP页面显示的Java代码
- java-页面数据生成图片
- 将图片转成base64字符串并在JSP页面显示的Java代码
- 在java中生成二维码,并直接输出到jsp页面
- 通过jsp页面生成Excel文件
- Jsp页面生成随机验证码图片
- 在java中生成二维码,并直接输出到jsp页面
- Java Web中通过配置Tomcat去掉JSP生成的空行