后端 存入的是byte[] 存进数据库的blob类型的字段中
@ResponseBody @RequestMapping(value = "plsczp" , method = RequestMethod.POST ) public String plsczp(String upload,String name,XjzpPage xjzpPage,@AuthenticationPrincipal UserDetails user,HttpServletRequest request){ String username = user.getUsername(); try { String[] filenames = name.split("\\."); String fileType = filenames[filenames.length-1]; String filename = name.substring(0,name.indexOf(fileType)-1); long timestamp=System.currentTimeMillis(); String jiaminame = filename + "@" + timestamp; String wjUrl = "D:"+File.separator+"xjzp" +File.separator + "dr"; String bsUrl = wjUrl +File.separator +jiaminame +"." + fileType; Map<String,String> xsxx = xjzpService.getXsxx(filename); if(xsxx == null){ throw new Exception(); } String[] base64 =upload.split(","); byte[] base64Body = new BASE64Decoder().decodeBuffer(base64[1]); //byte[] base64Body = new BASE64Decoder().decodeBuffer(upload); XszpXxEntity xszpXxEntity = new XszpXxEntity(); xszpXxEntity.setPylx("1"); xszpXxEntity.setXjid(xsxx.get("ID")); xszpXxEntity.setZplxdm(xjzpPage.getZplx()); xszpXxEntity.setZp(base64Body); xszpXxEntity.setZpbslj(bsUrl); xszpXxEntity.setZpmc(jiaminame + ".jpg"); InetAddress ip = InetAddress.getLocalHost(); String mac = getLocalMac(ip); xszpXxEntity.setIp(ip.toString()); xszpXxEntity.setMac(mac); xszpXxEntity.setCjr(username); xszpXxEntity.setZhxgr(username); xjzpService.insertXjXszpxx(xszpXxEntity); boolean s = GenerateImage(base64[1],bsUrl,wjUrl); return "1"; }catch (Exception e){ e.printStackTrace(); return "2"; } } public boolean GenerateImage(String imgStr, String imgFilePath,String wjUrl) { if (imgStr == null)// 图像数据为空 return false; BASE64Decoder decoder = new BASE64Decoder(); try { // Base64解码 byte[] bytes = decoder.decodeBuffer(imgStr); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) {// 调整异常数据 bytes[i] += 256; } } // 生成jpeg图片 File file = new File(wjUrl); //如果目录不存在则创建 if(!file.isDirectory()){ file.mkdirs(); } OutputStream out = new FileOutputStream(new File(imgFilePath)); out.write(bytes); out.flush(); out.close(); return true; } catch (Exception e) { e.getMessage(); return false; } }前端查看照片
@SuppressWarnings("all") @RequestMapping(value = "ckXszp" , method = RequestMethod.POST ) public Map ckXszp(@AuthenticationPrincipal UserDetails user, XjzpPage xjzpPage,HttpServletRequest request ) throws SQLException, UnsupportedEncodingException { Map map=new HashMap(); List<Map<String,String>> zplxList = xjzpService.queryZplx(); String xjid = xjzpService.getXjid(xjzpPage); xjzpPage.setXjid(xjid); List<XszpXxEntity> zpList = xjzpService.queryZp(xjzpPage); if(zpList.size()>0){ for (int i = 0; i < zpList.size(); i++) { XszpXxEntity xszpXxEntity = zpList.get(i); byte[] bdata = xszpXxEntity.getZp(); BASE64Encoder encode = new BASE64Encoder(); String s = encode.encode(bdata); s = "data:image/jpeg;base64," + s; xszpXxEntity.setZpstr(s); } } map.put("zplxList",zplxList); map.put("zpList",zpList); return map; }