Uploading mp4 from Android phone to Spring server results in file missing a few hundred bytes if bigger than 2MB

Tags: , , , ,



I have been trying to upload to a Java Spring server running on my laptop, using an app written in Xamarin forms, using a physical Redmi Android device.

But when I send out the multi-part request, if it is bigger than about 2MB, the file loses a few hundred bytes by the time it reaches the server.

For example, the original video file has 8,268,891 bytes. Sometimes the file that reaches the server will have 8,267,175 and sometimes 8,269,279 or some other random number.

I don’t know if it’s related to my Xamarin code, because this seems to happen whether I use multi-part requests or send it as a base64 string in a request.

But just in case, here is my multi-part Xamarin code

               var multipartContent = new MultipartFormDataContent();
                var videoBytes = new ByteArrayContent(file.GetStream().ToByteArray());
                multipartContent.Add(videoBytes, "file", file.Path.FileName());
                multipartContent.Add(new StringContent(serializedRequest, Encoding.UTF8, "application/json"), "request");

                content = multipartContent;
            }

            switch (type)
            {
                case RequestType.Post:
                    result = await client.PostAsync(_siteUrl + apiPath, content, cancellationToken);
                    break;

And my controller on the Spring server

  @RequestMapping(value = { RequestMappingConstants.MOBILE + RequestMappingConstants.UPLOAD + RequestMappingConstants.UPLOAD_VIDEO }, method = RequestMethod.POST)
  public @ResponseBody VideoUploadResponse uploadVideo(@RequestPart(value="request") VideoUploadRequest request, @RequestPart(value="file") MultipartFile file, HttpServletRequest httpRequest) {
      LOG.info("Inside video upload");
      return uploadService.uploadWelcomeVideo(request, file, httpRequest);

}

Also, my settings on the server:

multipart.maxFileSize= 100MB
multipart.maxRequestSize= 100MB
spring.servlet.multipart.enabled=true 
spring.servlet.multipart.file-size-threshold=2KB 
spring.servlet.multipart.max-file-size=200MB 
spring.servlet.multipart.max-request-size=215MB 
spring.servlet.multipart.resolve-lazily=false 

Again, this happens as long as the video file exceeds about 2MB. The corrupted file that reaches the server is unplayable. The server and client are running on the same wi-fi network.

I would be very grateful if you could help.

Answer

It turned out to be something wrong with my laptop or wireless network that was causing packet loss. Nothing to do with the code, as it was working when I tried it on a production server



Source: stackoverflow