0

After uploading file using IE isn't possible to delete folder the file is from, but the file can be deleted. After uploading by Firefox is not problem. How to unlock the directory?

my code:

DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
List<FileItem> uploadedItems = servletFileUpload.parseRequest(request);
if (uploadedItems != null && !uploadedItems.isEmpty()) {
    FileItem fileItem = uploadedItems.get(0);
    InputStream is = fileItem.getInputStream();
    long jobId = importService.importFileAsync(is, fileItem.getName());
    is.close();
    return jobId;
}

And in importFileAsync method I have:

public long importFileAsync(final InputStream inputStream, final String fileName) {
    Job job = new Job() {

        @Override
        protected void compute() throws ApplicationException {
            try (InputStreamReader inputReader = new InputStreamReader(inputStream, "UTF-8")){
                    //processing of data from file
            } catch(Exception e){
                    //processing of exception
            }
        }
    }   
    ...
}
Max Leske
  • 5,007
  • 6
  • 42
  • 54
maya
  • 49
  • 7

1 Answers1

0

If you use Apache Commons IO it's a one-liner:

FileUtils.deleteDirectory(dir);

See FileUtils.deleteDirectory()

Mike
  • 874
  • 1
  • 8
  • 15
  • But I want to let the user to manage his folders. It's not my aim to delete it programatically, but only "unlock", so user can delete it in filesystem if he want. – maya Jan 27 '14 at 18:20
  • you can't do this unless you release the lock on the folder, you can only do that by ceasing to write (close/end all streams) to that directory. – Mike Jan 27 '14 at 18:21
  • What else is necessary to close? I close inputStream got from FileItem. And by try-with-resource is automatically closed InputStreamReader, isn't it? – maya Jan 27 '14 at 18:27
  • I'm not so sure about that, you have a reference to the inputStream passed into the method, so your inputStream may be surviving the try/catch instance. I'm not 100% sure about that, it doesn't sound right to me, but since I can't see your code, and I can't see any other reason it would be locked, you should attempt manually closing the inputStream and see what happens then. (Trial and error until you get the lock off) – Mike Jan 27 '14 at 18:33