Action //新增人员 public void add() throws Exception{ BASE64Decoder decoder = new BASE64Decoder(); String imagpath= "D:\\test.jpg"; //Base64解码 byte[] b = decoder.decodeBuffer(GetImageStr(imagpath)); for(int i=0;i<b.length;++i) { if(b[i]<0) {//调整异常数据 b[i]+=256; } } //生成jpeg图片 String id = "410311199204204001"; FFFFFManager.add(b,id); System.out.print("ok"); } 可写成公共方法(单例模式) //图片转化成base64字符串 public String GetImageStr(String filePath) {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理 String imgFile = filePath;//待处理的图片 InputStream in = null; byte[] data = null; //读取图片字节数组 try { in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } //对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);//返回Base64编码过的字节数组字符串 } DAOimpl public void add(byte[] bz,String id) { Session session = this.getHibernateTemplate().getSessionFactory().openSession(); Transaction t = session.beginTransaction(); FFFFF fffff = new FFFFF(); fffff.setGrbh(id); fffff.setZp(bz); session.save(fffff); t.commit(); session.close(); }
这样就能够经过上一篇博文取到base64显示在前台了
java