private Collection<Part> parseMultipartWithCommonsFileUpload(HttpServletRequest request) throws IOException { if (sharedFileItemFactory.get() == null) { // Not a big deal if two threads actually set this up DiskFileItemFactory fileItemFactory = new DiskFileItemFactory( 1 << 16, (File) servletContext.getAttribute("javax.servlet.context.tempdir")); fileItemFactory.setFileCleaningTracker( FileCleanerCleanup.getFileCleaningTracker(servletContext)); sharedFileItemFactory.compareAndSet(null, fileItemFactory); } try { return new ServletFileUpload(sharedFileItemFactory.get()).parseRequest(request) .stream().map(FileItemPart::new).collect(Collectors.toList()); } catch (FileUploadException e) { throw new IOException(e.getMessage()); } }
/** * * @param servletContext * @param defaultEncoding * @param fileSizeMax * @param sizeMax * @param sizeThreshold: * @param tempDirPath */ public FileUploadSupport( ServletContext servletContext, String defaultEncoding, long fileSizeMax, long sizeMax, int sizeThreshold, String tempDirPath) { _defaultEncoding = defaultEncoding; _fileSizeMax = fileSizeMax; _sizeMax = sizeMax; _sizeThreshold = sizeThreshold; String defaultTempDirPath = servletContext.getRealPath(DEFAULT_TEMP_DIR); if(tempDirPath == null || tempDirPath.length() == 0) { tempDirPath = defaultTempDirPath; } _tempDir = new File(tempDirPath); if(!_tempDir.exists()) { boolean mkdirsSuccess = false; try { mkdirsSuccess = _tempDir.mkdirs(); } catch (Exception e) { } if(!mkdirsSuccess && !tempDirPath.equals(defaultTempDirPath)) { _tempDir = new File(defaultTempDirPath); if(!_tempDir.exists()) { _tempDir.mkdirs(); } } } _fileCleaningTracker = FileCleanerCleanup.getFileCleaningTracker(servletContext); //_diskFileItemFactory = new DiskFileItemFactory(sizeThreshold, _tempDir); //_diskFileItemFactory.setFileCleaningTracker(fileCleaningTracker); //createFileUpload(_defaultEncoding); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { boolean isMultipart = ServletFileUpload.isMultipartContent(req); HttpSession session = req.getSession(false); Integer userId = (int)session.getAttribute("id_user"); Integer orderId = (int)session.getAttribute("currentOrder"); PrintWriter out = resp.getWriter(); JsonObject jsonObject = new JsonObject(); if(isMultipart && userId != -1 && orderId != -1){ // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // Configure a repository (to ensure a secure temp location is used) ServletContext servletContext = this.getServletConfig().getServletContext(); FileCleaningTracker tracker = FileCleanerCleanup.getFileCleaningTracker(servletContext); factory.setFileCleaningTracker(tracker); File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir"); factory.setRepository(repository); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); try { List<FileItem> items = upload.parseRequest(req); for (FileItem imageFile : items){ byte[] imageByte = imageFile.get(); Order order = new Order(); order.setId(orderId); Image image = new Image(); image.setDataimage(imageByte); image.setOrder(order); new ImageDBManager().create(image); } jsonObject.addProperty("success", true); } catch (FileUploadException e) { jsonObject.addProperty("success", false); e.printStackTrace(); } } else { jsonObject.addProperty("success", false); } out.append(jsonObject.toString()); out.flush(); }