Forty
Forty

Reputation: 430

How to delete PrimeFaces UploadedFile temp file after processing?

PrimeFaces UploadedFile only exposes the InputStream, not the File itself. How can I delete it after processing the stream?

Upvotes: 7

Views: 9405

Answers (2)

AZ_
AZ_

Reputation: 21899

It will remove some file but not all files until you Restart Server. I am working with JBoss 2.4.3 and Added Support of JSF 2.0 in Servlet 2.4. Please read complete detail on my other question here

Well You need to write your own filter Read more about FileCleaningTracker on Apache website.

Step 1:

Extend your class with org.primefaces.webapp.filter.FileUploadFilter and override following method

@Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        boolean isMultipart = ServletFileUpload.isMultipartContent(httpServletRequest);
        if (isMultipart) {
            if (logger.isLoggable(Level.FINE))
                logger.fine("Parsing file upload request");

            FileCleaningTracker fileCleaningTracker = FileCleanerCleanup.getFileCleaningTracker(ContextConfigServlet.SERVLET_CONTEXT);
            DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
            diskFileItemFactory.setFileCleaningTracker(fileCleaningTracker);

            if (thresholdSize != null)
                diskFileItemFactory.setSizeThreshold(Integer.valueOf(thresholdSize).intValue());
            if (uploadDir != null)
                diskFileItemFactory.setRepository(new File(uploadDir));

            ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
            MultipartRequest multipartRequest = new MultipartRequest(httpServletRequest, servletFileUpload);

            if (logger.isLoggable(Level.FINE))
                logger.fine("File upload request parsed succesfully, continuing with filter chain with a wrapped multipart request");

            filterChain.doFilter(multipartRequest, response);

        } else {
            filterChain.doFilter(request, response);
        }

    }

I have a Servlet that is called when context is initialized and getting ServletContext from there you can do like request.getSession.getServletContext...Please validate this

Step2:

Goto your web.xml and add your filter calss in param

<filter-class>com.sf.server.filter.FileUploadFilter_</filter-class>     

All done now it will remove all the temp files.

Upvotes: 2

BalusC
BalusC

Reputation: 1109112

PrimeFaces uses Apache Commons FileUpload under the covers for this. It will create the file as a temporary file and hence the file will already be automatically deleted if there are no open File nor InputStream references to it when the Java Garbage Collector runs.

So if you can make absolutely sure that you close the InputStream after processing (in the finally block!), then you don't need to worry about cleanup.

Upvotes: 9

Related Questions