mp3agic library java.io.IOException: Cannot delete temporary mp3 file from \Tomcat\localhost\ROOT\file.tmp

432 Views Asked by At

The code works completly fine but I get this exception:

Caused by: java.io.IOException: Cannot delete 
C:\Users\noob234\AppData\Local\Temp\tomcat.8080.5659248991534133251\work\Tomcat\localhost\ROOT\upload_1723fa92_3db6_497a_9bdc_ac298b991fc8_00000000.tmp

I tried to somehow delete it or just add a sleep for one second, but unfortunatley I can't get rid of it.

I am trying to get some fields from a multipart file. That multipart file is a mp3 file. Here is a snippet of the code:

public void upload (MultipartFile file){
// ....

String fileName = file.getOriginalFilename();
if ("mp3".equals(extension)) {
            try {
                Mp3File mp3file = new Mp3File(fileName);
                sampleRate = mp3file.getSampleRate();
            } catch (Exception e) {
                throw new Mp3Exception("Couldn't get mp3 fields: " + e.getMessage())
            }
    }

// ....

Please keep in mind that I use in this project Java 8 and Gradle. Here is the dependency that I use:

    implementation 'com.mpatric:mp3agic:0.9.1'
1

There are 1 best solutions below

0
On BEST ANSWER

Fixed it:

implementation 'org:jaudiotagger:2.0.3'
    public void upload(MultipartFile file){
        // ....
        String fileName = file.getOriginalFilename();
        if ("mp3".equals(extension)) {
            try {
                File tempFile = File.createTempFile("temp", ".mp3");
                file.transferTo(tempFile);
                AudioFile audioFile = AudioFileIO.read(tempFile);
                sampleRate = audioFile.getAudioHeader().getSampleRateAsNumber();
                duration = audioFile.getAudioHeader().getTrackLength();
            } catch (Exception e) {
                throw new Mp3Exception("Couldn't get mp3 fields: " + e.getMessage())
            }
        }
        // ....
    }