valik
valik

Reputation: 2094

How to detect file type for upload in spring controller picture/video

I have two entities photo and video, basically i need a controller that can upload both picture and video. an eg scenario is i execute the controller it opens my file and if i select a video it is handled as an mp4 with the video entity and if i select a picture it uses the photo entity and handles it as an image. Both entities have Multipartfile attribute to denote image and video.

Baically i have seen this link that has an answer for uploading a video How to Implement HTTP byte-range requests in Spring MVC

Another example would be in social apps we use one click to upload either a photo or a video

This is what i have currently

@RequestMapping(value = "/Upload", method = RequestMethod.POST)
public String  FileUpload(HttpServletRequest request,
  @RequestParam("fileUpload")  MultipartFile[] fileUpload) throws    Exception {


}

I would like to use the MultipartFileSender from the above link but not sure how handle it with two different enities one for video and photo

currently i have this

  @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String Post(@Nullable Photo photo, @Nullable Video video,
                       HttpServletRequest request, HttpServletResponse response) {

     String ext1 = FilenameUtils.getExtension("/path/to/file/foo.txt");
     if(ext1.matches("png")) {

       MultipartFile bookImage = photo.getImage();
       try {
           byte[] bytes = bookImage.getBytes();
           String name = photo.getId() + ".png";
           BufferedOutputStream stream =
               new BufferedOutputStream(
                   new FileOutputStream(new File("src/main/resources/static/image/book/" + name)));
           stream.write(bytes);
           stream.close();

           photoRepository.save(photo);
       } catch (Exception e) {
           e.printStackTrace();
       }

      }  else {
         /*

          */
          MultipartFile videoFile = video.getVideo();
         /**
          * not sure how to  continue about this
the  class bellow MultipartFileSender can be found here https://stackoverflow.com/questions/28427339/how-to-implement-http-byte-range-requests-in-spring-mvc  i am using that because i need a byte range request for the video upload 
          */
         MultipartFileSender.fromFile(File( ))
                              .with(request)
             .with(response)
             .serveResource();
   }
      return null;
 }

Upvotes: 3

Views: 1759

Answers (1)

Zeusox
Zeusox

Reputation: 8448

There are different ways to do this. I have done something similar, but I kinda used some tricks to get to a result similar to yours.

1- create 2 separate array list with all possible video and image extensions

1 - Create a method that would get your media's file extension

2 - create a method that would compare the file extension you get against your lists of arrays that includes all possible video and image extensions.

This way you will separate between what is video and image and handle them differently. Hope this helps !

Upvotes: 1

Related Questions