how to upload video in spring boot

I want to upload video using spring boot and I followed a certain tutorial in order to achieve that but I am getting this error in the logcat when I tried to upload the video.

Cannot delete C:\Users\HP\AppData\Local\Temp\tomcat.8080.13279303856254081018\work\Tomcat\localhost\ROOT\upload_9bbd6e73_7a76_4ae8_bd76_5e33db6d9dab_00000000.tmp.

I need some help on how to resolve it please

This is my code

    @RestController
    public class VideoController {
    
    @Autowired
        private VideoService videoService;
        @Autowired
        private FileService fileService;
    
    @PostMapping("/post/{id}")
        public VideoModel uploadVideo(@RequestParam("video")MultipartFile video, @PathVariable Integer id) throws IOException, DataNotFoundException {
            VideoModel videoModel = videoService.getById(id);
            FileModel fileModel = fileService.uploadVideo(path,video);
            videoModel.setVideoName(fileModel.getVideoFileName());
            return videoService.updateVideo(videoModel,id);
        }
    }
    
    public interface VideoService {
    
        public VideoModel createVideo(VideoModel videoModel);
        public VideoModel getById(Integer id) throws DataNotFoundException;
    }
    
    public interface FileService {
    
        FileModel uploadVideo(String path, MultipartFile file) throws IOException;
        InputStream getVideoFile(String path, String fileName,int id) throws FileNotFoundException;
    }
    
    
    @Service
    public class VideoServiceImplementation implements VideoService {
    
        @Autowired
        private VideoRepository videoRepository;
        @Override
        public VideoModel createVideo(VideoModel videoModel) {
            return videoRepository.save(videoModel);
    
        }
    }
    
    
    @Service
    public class FileServiceImplementation implements FileService {
        @Override
        public FileModel uploadVideo(String path, MultipartFile file) throws IOException {
            FileModel fileModel = new FileModel();
            String fileName = file.getOriginalFilename();
            //generate some random and unique name for each file
            String randomId = UUID.randomUUID().toString();
            String finalName = randomId.concat(fileName).substring(fileName.indexOf("."));
            //file full path
            String filePath = path + File.separator + finalName;
            //creating the directory to save the file to
            File f = new File(path);
            Files.copy(file.getInputStream(), Paths.get(filePath));
            fileModel.setVideoFileName(finalName);
            return fileModel;
        }
    
        @Override
        public InputStream getVideoFile(String path, String fileName, int id) throws FileNotFoundException {
            String fullPath = path + File.separator + fileName;
            InputStream inputStream = new FileInputStream(fullPath);
            return inputStream;
        }
    }

application.properties

    spring.datasource.url=jdbc:mysql://localhost:3306/online_learningdb
    spring.datasource.username=root
    spring.datasource.password=elijah
    spring.jpa.show-sql=true
    spring.jpa.hibernate.ddl-auto=update
    
    spring.servlet.multipart.enabled=true
    spring.servlet.multipart.file-size-threshold=2KB
    spring.servlet.multipart.max-file-size=300MB
    spring.servlet.multipart.max-request-size=400MB
    
    spring.jackson.parser.allow-unquoted-control-chars=true
    project.video=videos/
    project.url=http://localhost:
    
    spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER

Leave a Comment