Skip to content

Chunking and uploading file Gives Exception "The process cannot access the file because it is being used by another process." #2

@taufiquealam007

Description

@taufiquealam007

Hi,

I am sending one file from browser and uploading to server, it is chunking and uploading to the server.

But while uploading it is giving exception

you can get the github link for the implementation:

) https://github.com/tus/tus-js-client
Spring server implementation
2) https://github.com/thomasooo/spring-boot-tus

java.nio.file.FileSystemException: storage\5b87e2a903751c247c164465: The process cannot access the file because it is being used by another process.

at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86) ~[?:1.8.0_144]
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) ~[?:1.8.0_144]
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102) ~[?:1.8.0_144]
at sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:269) ~[?:1.8.0_144]
at sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(AbstractFileSystemProvider.java:108) ~[?:1.8.0_144]
at java.nio.file.Files.deleteIfExists(Files.java:1165) ~[?:1.8.0_144]
at java.nio.file.Files.copy(Files.java:3004) ~[?:1.8.0_144]
at tusserver.storage.services.StorageServiceImpl.processStream(StorageServiceImpl.java:84) ~[classes/:?]
at tusserver.StorageController.processPatch(StorageController.java:186) ~[classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_144]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_144]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_144]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE
Code :

public int processStream(String uuid, InputStream inputStream) throws Exception {
String filename = StringUtils.cleanPath(uuid);
File file = new File(storageDir.resolve(filename).toString());

if (!file.isFile()){
    new FileOutputStream(file).close();
    if(!file.isFile()){
        log.error("Cannot create new file");
        throw new TusPermissionDeniedException("Cannot create new file");
    }
}

InputStream storageFile;
try{
    storageFile = new FileInputStream(file);
}catch(IOException e){
    log.error("Cannot read old file");
    throw new TusPermissionDeniedException("Cannot read old file");
}

// File fileToBeInstalled = new File(StorageServiceImpl.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
// Files.copy(fileToBeInstalled.toPath(), storageDir.resolve(filename), StandardCopyOption.REPLACE_EXISTING);
// fileToBeInstalled.delete();

storageFile = new SequenceInputStream(storageFile, inputStream);
// at this position giving exception
Files.copy(storageFile, storageDir.resolve(filename), StandardCopyOption.REPLACE_EXISTING);

file = new File(storageDir.resolve(filename).toString());

return (int) file.length();

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions