天天看點

jsp 頁面顯示圖檔

/**
         * 根據工單Id 查詢該條工單所對應的峰圖
         * overriding
         * @see org.deyi.sc.service.PcrProjectService#selectPic(javax.servlet.http.HttpServletRequest, java.lang.String)
         * @author: wanght
         * 2018-2-24 上午10:00:59
         * @param req
         * @param orderId
         * @return 
         *
         */
        public Object selectPic(HttpServletRequest req, String orderId) {
            List<Map<String, Object>> picMap = new ArrayList<Map<String,Object>>();
            Map<String,Object> pdata = new HashMap<String,Object>();
            pdata.put("orderId", orderId);
            //通過orderId查詢序列峰圖,封圖
            List<Map<String, Object>> maps = pdao.selectPic(pdata);
            String path = picPath.getProperty("uploadPic"); 
            for (Map<String, Object> map : maps) {
                String pnameString = path+File.separator+map.get("pname");
                map.put("pname","data:image/png;base64,"+new Base64().encodeToString(getBytes(pnameString)) );
                picMap.add(map);
            }
            pdata.put("rows", picMap);
            return pdata;
        }
        /** 
         * 獲得指定檔案的byte數組 
         */  
        private byte[] getBytes(String filePath){  
            byte[] buffer = null;  
            try {  
                File file = new File(filePath);  
                FileInputStream fis = new FileInputStream(file);  
                ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);  
                byte[] b = new byte[1000];  
                int n;  
                while ((n = fis.read(b)) != -1) {  
                    bos.write(b, 0, n);  
                }  
                fis.close();  
                bos.close();  
                buffer = bos.toByteArray();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
            return buffer;  
        }