從服務器中讀取圖片至頁面中


圖片的數據數據庫中存儲:

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM