Java Code Examples for org.apache.commons.fileupload.disk.DiskFileItemFactory.setFileCleaningTracker()

The following are Jave code examples for showing how to use setFileCleaningTracker() of the org.apache.commons.fileupload.disk.DiskFileItemFactory class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: oryx2   File: AbstractOryxResource.java   View Source Code Vote up 6 votes
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());
  }
}
 
Example 2
Project: Kvantum   File: KvantumFileUpload.java   View Source Code Vote up 5 votes
private static FileItemFactory getDefaultFileItemFactory(final int threshold)
{
    final DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
    diskFileItemFactory.setFileCleaningTracker( new FileCleaningTracker() );
    diskFileItemFactory.setSizeThreshold( threshold );
    return diskFileItemFactory;
}
 
Example 3
Project: ProteanBear_Java   File: FileUploader.java   View Source Code Vote up 5 votes
private ServletFileUpload getFileUploadComponent()
{
    DiskFileItemFactory dif=new DiskFileItemFactory();

    if(factorySizeThreshold!=DEFAULT_SIZE_THRESHOLD)
    {
        dif.setSizeThreshold(factorySizeThreshold);
    }
    if(factoryRepository!=null)
    {
        dif.setRepository(new File(factoryRepository));
    }
    if(factoryCleaningTracker!=null)
    {
        dif.setFileCleaningTracker(factoryCleaningTracker);
    }

    ServletFileUpload sfu=new ServletFileUpload(dif);

    if(sizeMax!=NO_LIMIT_SIZE_MAX)
    {
        sfu.setSizeMax(sizeMax);
    }
    if(fileSizeMax!=NO_LIMIT_FILE_SIZE_MAX)
    {
        sfu.setFileSizeMax(fileSizeMax);
    }
    if(servletHeaderencoding!=null)
    {
        sfu.setHeaderEncoding(servletHeaderencoding);
    }
    if(servletProgressListener!=null)
    {
        sfu.setProgressListener(servletProgressListener);
    }

    return sfu;
}
 
Example 4
Project: REST-framework   File: FileUploadSupport.java   View Source Code Vote up 5 votes
private ServletFileUpload createFileUpload(String encoding) {
	DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(_sizeThreshold, _tempDir);
	diskFileItemFactory.setFileCleaningTracker(_fileCleaningTracker);

	ServletFileUpload fileUpload = new ServletFileUpload(diskFileItemFactory);
	fileUpload.setHeaderEncoding(encoding);
	fileUpload.setFileSizeMax(_fileSizeMax);
	fileUpload.setSizeMax(_sizeMax);
	
	//_fileUploadEncodingMap.put(fileUpload.getHeaderEncoding(), fileUpload);
	
	return fileUpload;
}
 
Example 5
Project: Java-education   File: ImageServlet.java   View Source Code Vote up 4 votes
@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();


}