I have been searching this topic for quite a while, and haven't found anything that has been able to solve my problem.. so I turn to you!
I have a JSP where I open a file dialog box to select a file. Previously, I used this to upload the file to a specified directory (in my code). This works fine. I am now trying to use the same code to delete that same file by selecting it in the appropriate directory and passing it off to the servlet, which I included below. I am using the Apache Common FileUpload library to do this.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // checks if the request actually contains upload file if (!ServletFileUpload.isMultipartContent(request)) { // if not, we stop here return; } // configures some settings DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload delete = new ServletFileUpload(factory); // constructs the directory path to delete file String deletePath = UPLOAD_DIRECTORY; // parses the request's content to extract file data List formItems = delete.parseRequest(request); Iterator iter = formItems.iterator(); // iterates over form's fields while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); // processes only fields that are not form fields if (!item.isFormField()) { String fileName = new File(item.getName()).getName(); String filePath = deletePath + File.separator + fileName; File storeFile = new File(filePath); //File storeFile = new File("C:\\temp\\discLogo.txt"); // deletes the file on disk boolean erased = storeFile.delete(); } } UPLOAD_DIRECTORY is where I am storing my files from my upload JSP. The delete method works fine if I uncomment the line I commented out for storeFile with the hardcoded directory, as long as I select a DIFFERENT FILE in the directory initially. This leads me to believe the HttpServletRequest is holding the file in memory somewhere.
Is this correct? is there any way I can release it so I can delete the file I select initially? Or is there a much simpler way to do this?
Thanks!