首页 > 代码库 > blob数据输出到页面显示图片
blob数据输出到页面显示图片
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="http://www.mamicode.com/"> <title>My JSP ‘showImg.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> </head> <body> <table> <tr> <td>111</td> <td><img src=http://www.mamicode.com/‘#/‘" /showimg.do?id=1" width="200px" height="180px" ></td> </tr> </table> </body> </html>
@RequestMapping(value = "/uploadimage.do") public void uploadPhoto(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,HttpServletResponse response) throws Exception { try{ byte[] data = file.getBytes(); int id = uploadDao.FileUpload(data, file.getOriginalFilename()); } catch(Exception e){ e.printStackTrace(); } } @RequestMapping(value = "/showimg.do") public void showimg(HttpServletRequest request,HttpServletResponse response) throws Exception { int id = Integer.parseInt(request.getParameter("id")); System.out.println(id); List<Object> list = uploadDao.ShowImg(id); Blob blob=(Blob) list.get(0); int length = (int) blob.length(); byte[] bImage = new byte[length]; InputStream is = new BufferedInputStream(blob.getBinaryStream()); is.read(bImage, 0, length); OutputStream out = response.getOutputStream(); out.write(bImage); out.flush(); out.close(); is.close(); }
public List<Object> ShowImg(int id) { String sql = "select name,img from img where id = "+id+""; List<Object> list=new ArrayList<Object>(); Connection conn=null; Statement state=null; ResultSet rs = null; try { conn = jdbcTemplate.getDataSource().getConnection(); state=conn.createStatement(); rs=state.executeQuery(sql.toString()); if (rs.next()) { Blob blob = rs.getBlob("img"); list.add(0, blob); } } catch (SQLException e) { e.printStackTrace(); } return list; }
本文出自 “骑猴上树” 博客,请务必保留此出处http://qihoushangshu.blog.51cto.com/7872138/1543647
blob数据输出到页面显示图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。