上一篇解决了java+jquery+webcam调用本地摄像头拍照功能的实现,这一篇主要解决如何把拍照后生产的图片保存到blob字段的问题了,直接贴代码了:
工具类图片转byte字节数组:
//图片到byte数组 public static byte[] image2byte(String path){ byte[] data = null; ByteArrayOutputStream output=null; FileImageInputStream input = null; try { input = new FileImageInputStream(new File(path)); output = new ByteArrayOutputStream(); byte[] buf = new byte[10240]; int numBytesRead = 0; while ((numBytesRead = input.read(buf)) != -1) { output.write(buf, 0, numBytesRead); } data = output.toByteArray(); }catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException ex1) { ex1.printStackTrace(); }finally{ try { output.close(); input.close(); } catch (IOException e) { e.printStackTrace(); } } return data; }
保存图片:
//上传图片 String savePath = request.getRealPath("/") + "//img//001.jpg"; File file_txt = new File(savePath); if (file_txt.exists()) { byte[] bytes = ImageTools.image2byte(savePath); //System.out.println(bytes.length); if(null!=bytes && bytes.length>0){ entity.setPic(bytes); code =***Service.insert***(entity).getExcuteCode(); } }else{ code = new ExcuteCode("-99", "保存失败:不存在照片文件!"); }