// Last edited on 2014-12-15 21:33:42 by stolfilocal @Override public void insertImage(User sessionUser, InputStream fileStream) { // TODO Auto-generated method stub try { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); getConnection(); PreparedStatement ps = getStatement("INSERT INTO profilePicture (user_id, filename, last_modified, photo)"+ "VALUES (" + sessionUser.getIndexDB() + ", '" + sessionUser.getIndexDB() + ".jpg', '" + dateFormat.format(new Date(Calendar.getInstance().getTimeInMillis())) + "', ?);"); ps.setBlob(1, fileStream); ps.execute(); commit(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void loadProfileImage(User user, ServletContext context) { String filePath = context.getRealPath("/") + "pub/profileImages"; getConnection(); try { ResultSet rs = getStatement("SELECT * FROM profilePicture WHERE user_id =" + String.valueOf(user.getIndexDB())).executeQuery(); if(rs.next()){ Blob blob = rs.getBlob("photo"); InputStream inputStream = blob.getBinaryStream(); OutputStream outputStream = null; try { outputStream = new FileOutputStream(filePath); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } int bytesRead = -1; byte[] buffer = new byte[1024]; try { while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } inputStream.close(); outputStream.close(); System.out.println("Arquivo salvo em " + filePath); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }