从服务器中读取图片至页面中

图片的数据数据库中存储:

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();
            }
        }
    }

 

posted @ 2020-03-15 01:30  shouyaya  阅读(1709)  评论(0编辑  收藏  举报