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

huangapple go评论77阅读模式
英文:

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

问题

我一直在尝试使用Xamarin Forms编写的应用程序,在我的笔记本电脑上运行的Java Spring服务器上上传文件,使用一台物理的Redmi Android设备。

但是,当我发送多部分请求时,如果文件大于约2MB,文件在到达服务器时会丢失几百字节。

例如,原始视频文件大小为8,268,891字节。有时到达服务器的文件大小为8,267,175,有时为8,269,279或其他随机数。

我不知道这是否与我的Xamarin代码有关,因为无论我是使用多部分请求还是将其作为base64字符串发送请求,这种情况似乎都会发生。

但是为了以防万一,这是我的多部分Xamarin代码:

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;

以及我的Spring服务器上的控制器:

@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);
}

还有我的服务器设置:

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 

再次强调,只要视频文件超过约2MB,就会发生这种情况。到达服务器的损坏文件无法播放。

服务器和客户端在同一Wi-Fi网络上运行。

如果您需要进一步的帮助,请告诉我。

英文:

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.

答案1

得分: 1

我已经发现您还需要调整Tomcat和/或Jetty(根据情况)的设置:

server.jetty.max-http-form-post-size: 100MB                                      

server.tomcat.max-http-form-post-size: 100MB                                     
server.tomcat.max-swallow-size: -1
英文:

I have found that you also need to adjust the Tomcat and/or Jetty (as appropriate) settings:

server.jetty.max-http-form-post-size: 100MB                                      

server.tomcat.max-http-form-post-size: 100MB                                     
server.tomcat.max-swallow-size: -1

答案2

得分: 0

有关我的笔记本电脑或无线网络出现问题导致数据包丢失的情况,原来与代码无关,因为当我在生产服务器上尝试时,代码是正常工作的。

英文:

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

huangapple
  • 本文由 发表于 2020年8月4日 21:42:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63248209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定