7

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

Forty
  • 420
  • 1
  • 5
  • 10

2 Answers2

9

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I am closing it IOUtils.closeQuietly(in) but still not removing .temp files – AZ_ Oct 25 '13 at 02:51
  • 1
    @AZ_: they are not *immediately* removed but at intervals. – BalusC Oct 25 '13 at 09:10
  • Yeah it deletes some after interval but Cache a lot of files till you don't restart server and I am uploading Gigs of Video so it is occupying my space until I restart Jboss and Restarting Jboss is not an option for me :-) – AZ_ Oct 26 '13 at 06:32
2

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.

Community
  • 1
  • 1
AZ_
  • 21,688
  • 25
  • 143
  • 191