// Last edited on 2014-12-15 16:51:46 by stolfilocal @Override public void processFileUploadReq(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws IOException, ServletException { // TODO Auto-generated method stub response.setContentType("text/html;charset=UTF-8"); // Create path components to save the file ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final String path = request.getServletContext().getRealPath("/") + "pub/img/profilepics"; final Part filePart = request.getPart("file"); final String fileName = getFileName(filePart); String cookie = getCookieValue(request, COOKIE_NAME); User user = (User)getUserFromCookie(cookie); OutputStream out = null; InputStream filecontent = null; final PrintWriter writer = response.getWriter(); try { out = new FileOutputStream(new File(path + File.separator + fileName)); filecontent = filePart.getInputStream(); this.database.insertImage(user, filecontent); /*long read = 0; final byte[] bytes = new byte[1024]; while ((read = filecontent.read(bytes)) != -1) { out.write(bytes, 0, read); } writer.println("New file " + fileName + " created at " + path); //LOGGER.log(Level.INFO, "File{0}being uploaded to {1}", //new Object[]{fileName, path});*/ } catch (FileNotFoundException fne) { writer.println("You either did not specify a file to upload or are " + "trying to upload a file to a protected or nonexistent " + "location."); writer.println("
ERROR: " + fne.getMessage()); //LOGGER.log(Level.SEVERE, "Problems during file upload. Error: {0}", //new Object[]{fne.getMessage()}); } finally { if (out != null) { out.close(); } if (filecontent != null) { filecontent.close(); } if (writer != null) { writer.close(); } } }