protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { FileInputStream in = null; try { File file = new File(getServletContext().getRealPath("/<thePath>/<fileName>")); ByteBuffer buff = ByteBuffer.wrap(new byte[(int)image.length() + (image.length() % 1024 > 0 ? 1024 : 0)]); byte[] b = new byte[1024]; in = new FileInputStream(file); while(in.read(b) != -1) { buff.put(b); } response.getWriter().write(Base64.encode(buff.array())); } catch(Exception e) { String msg = "Fehler beim lesen des Bildes"; LOG.log(Level.SEVERE, msg, e); response.sendError(500, msg); return; } finally { if(in != null) in.close(); } }
CAUTION! This should only work for files up to 2GB: setting capacity of ByteBuffer only takes an int.