Home > database >  Spring Thymeleaf Video Playback
Spring Thymeleaf Video Playback

Time:06-16

I am creating a web application using spring mvc and thymeleaf and I seem to be running into an issue. I have a feature to allow a user to upload a video from the filesystem to the spring workspace when the app is running. The video is then displayed on the page.

The problem I am having is the video doesn't play when I upload it if the app is running. The upload function works and the video is added to the "videos" folder. If i restart the server, the video plays automatically because it was already in the folder.

I have a similar function for images and this line in the properties file to reload the static folder when an image is upload. This works and images are displayed but for some reason videos dont play.

spring.web.resources.static-locations[0]=file:src/main/resources/static/
spring.web.resources.static-locations[1]=classpath:/static/

Method to save video to workspace

    @RequestMapping(method = RequestMethod.POST, path = "/projects/save/video")
    public String uploadVideo(@RequestParam("file") MultipartFile file, RedirectAttributes attributes) throws InterruptedException {
        // check if file is empty
        if (file.isEmpty()) {
            System.out.println("No file");
            return "redirect:/user/projects";
        }
        // normalize the file path
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());

        // save the file on the local file system
        try {
            Path path = Paths.get(VID_UPLOAD_DIR   fileName);
            Files.copy(file.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        String part="../videos/" fileName;
        System.out.println(file.getSize()   " <---[]---> "   part);

        member.setVideo(part);
        return "redirect:/user/projects";
    }

So basically video only plays if it's in the resource/static/videos folder before the app is running. If its uploaded in while the app is already running it doesn't play when browser is refreshed.

<video>
    <source th:src="@{${video.path}}" type=video/mp4>
</video>

I would think the folder is locked but like I said images that are uploaded while the app is running are displayed.

CodePudding user response:

You probably need to close the inputstream. Use try-with-resources:

try(InputStream inputStream = file.getInputStream()) {
   Path path = Paths.get(VID_UPLOAD_DIR   fileName);
   Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
}
catch (IOException e) {
  e.printStackTrace();
}

CodePudding user response:

So I actually managed to find a not so pretty solution for this. This link below gave me the idea.

How to refresh src/main/resources folder while executing the code?

Based on this I observed that all the other static resources (images, js, css) had a version in target/*/classes but because I was starting with an empty video folder none is created when the app starts.

When i add a video, the video is then added to my videos static folder but its not displayed on the browser because it doesnt get created in the class folder. This is why when I restart tomcat with the video still in the folder it automatically plays.

Solution

Create a directory in the /target/classes/static folder (might be slightly different for others)

Then copy the video from your static folder to the static folder in classes.

!(Important to copy from the first video uploaded to your static folder. If you copy from input stream, it wont work)

    private final String VID_UPLOAD_DIR = "C:\\Users\\adams\\IdeaProjects\\demo\\src\\main\\resources\\static\\videos\\";
    private final String CLASS_DIR = "C:\\Users\\adams\\IdeaProjects\\demo\\target\\classes\\static\\videos\\";

            File directory = new File(CLASS_DIR);
            if (! directory.exists()) {
                directory.mkdir();
                System.out.println("Making Dir");
            }
            Path classPath = Paths.get(CLASS_DIR   fileName);
            Files.copy(path, classPath, StandardCopyOption.REPLACE_EXISTING);

Hope this helps anyone having the same issue.

  • Related