1

I am trying to delete a file after uploading a file to a cloud storage location using a rest call by attaching the file in headers.(upload process is successful). but i am unable to delete the file after upload got successful. is there any way i can upload a large file(max 150 MB) through rest post call and afterwards i need to delete the file.

this is a production application so i need to opt for a best solution. i am using this above approach and also thinking to test with another approach - converting the file to MultipartFile and then uploading it to cloud . and then delete the file and MultipartFile afterwards.


public void fileHandlingService(DocumentFileDetails documentFileDetails,UploadFileRequest uploadFileRequest){
 File file = new File(documentFileDetails.getFolderDirectory() + documentFileDetails.getFileName()
                + documentFileDetails.getFileExtension());
 uploadingFile(file,uploadFileRequest);

 //delete the file whether the upload is success or failure

 if (file.exists()) {
     //TODO unable to delete file here upload is successful but file resource is getting locked i guess
    FileUtils.forceDelete(file);
 }

}



public boolean uploadingFile(File file, UploadFileRequest uploadFileRequest) throws IOException {
        Resource fileResource=new FileSystemResource(file);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        PresignedFields fields = uploadFileRequest.getPresignedPost().getFields();
        //attaching some file details to the body
        //body.....

        body.add("Content-Disposition", "attachment; filename=\"" + uploadFileRequest.getFileName() + "\"");
        body.add("Content-Type", uploadFileRequest.getDocumentType());
        
        //file is getting upload to the cloud (need to upload file having size 150 mb)
        body.add("file", fileResource);

    

        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
        String serverUrl = uploadFileRequest.getPostUrl();
        try {
            ResponseEntity<Object> response = restTemplate.postForEntity(serverUrl, requestEntity, Object.class);
        
            if (response.getStatusCode() == HttpStatus.NO_CONTENT) {
                log.info("Successfully uploaded document", null);
                return true;
            }

            return false;
        } catch (Exception exception) {
            log.error("Exception when uploading file" + exception.getMessage(), null);
            throw new Exception(ErrorCode.FAILED_UPLOAD.getCode());
        }

    }

} 

using above code it is failing at this line with IO exception (guessing that fileSystemResource is not releasing the lock on the file)

FileUtils.forceDelete(file);

Exception:

java.io.IOException: Cannot delete file: D:\TempFolder\TestFolder\Testfile.txt
    at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:1344) ~[commons-io-2.11.0.jar:2.11.0]
Chittu
  • 21
  • 2
  • 4

1 Answers1

0

Identified my own mistake, so answering my own question.

FileUtils.forceDelete(file);

will delete the file if that file is not opened or not getting used any where by the code or machine.

In my case this file is being used by some cloud storage sync operation (like google drive) which is using this file to upload . so FileUtils is unable to delete that file.

fixed the issue by not syncing the folder.

Chittu
  • 21
  • 2
  • 4