从服务器中读取图片至页面中
图片的数据数据库中存储:
else if ("/cake/getImg.do".equals(req.getServletPath())) { String idStr = req.getParameter("id"); Cake cake = cakeService.getCakeImg(Long.valueOf(idStr)); //通过Mybatis获取图片的数据,封装在cake这个类中 try { resp.setContentType("multipart/form-data"); if (null != cake && null != cake.getSmallImg()) { InputStream in = new ByteArrayInputStream(cake.getSmallImg()); ServletOutputStream out = resp.getOutputStream(); byte[] b = new byte[1024]; int length = in.read(b); while (length != -1) { out.write(b); length = in.read(b); } out.flush(); out.close(); in.close();resp.flushBuffer(); } } catch (IOException e) { e.printStackTrace(); } } }